fstab
fstab

Reputation: 5029

python echoing commands contained in a script?

I wonder if it's possible to call the python interpreter in such a way that the python statements are printed (echoed) to standard output instead of being just silently executed.

any ideas?

Upvotes: 0

Views: 137

Answers (2)

Wang
Wang

Reputation: 8214

I have a script file:testScript.py

import sys
for _i in sys.path:
    print _i

Then I can use the command:

$ python -m trace -t testScript.py

The result will looks like this:

--- modulename: testScript, funcname: <module>
testScript.py(1): import sys
testScript.py(2): for _i in sys.path:
testScript.py(3):     print _i

testScript.py(2): for _i in sys.path:
testScript.py(3):     print _i
/usr/lib/python2.7
testScript.py(2): for _i in sys.path:
testScript.py(3):     print _i
/usr/lib/python2.7/plat-linux2
testScript.py(2): for _i in sys.path:
testScript.py(3):     print _i
/usr/lib/python2.7/lib-tk

...

testScript.py(2): for _i in sys.path:
testScript.py(3):     print _i
/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode
testScript.py(2): for _i in sys.path:
--- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

I have a module file: test.py

import sys
def test():
    for _i in sys.path:
        print _i

then I do this:

$ python
>>> import imp
>>> mytest=imp.load_source('mytest','test.py')  

use the trace module to trace your code:

>>> import trace
>>> tracer=trace.Trace()
>>> tracer.run('mytest.test()')

result will be:

--- modulename: test, funcname: test
test.py(3):     for _i in sys.path:
...
test.py(3):     for _i in sys.path:
test.py(4):         print _i
/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode
test.py(3):     for _i in sys.path:
--- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

Upvotes: 1

jbertino
jbertino

Reputation: 11

It sounds like you need a debugger. If you are not comfortable with python debug or pudb then you could try one of the myriad visual debuggers out there.

Aside from setting a breakpoint at the start of the code and "stepping into" each line, I am not aware of a debugger that prints each statement to an output as it is interpreted.

Upvotes: 1

Related Questions