0

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?

1
  • It is not clear what do you want to achieve. You can always look at your script. If you want to know which step the running script at, please modify your post to be more clear. Commented Jun 2, 2014 at 1:54

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

Thanks for your answer. Any idea on how to use a debugger in order to achieve what I want?
Not to output the statements to the screen, but if you used a visual debugger you could set a breakpoint at the start of the code and then "step into" the commands one by one, which will highlight the lines one at a time as they are interpreted on most common visual debuggers.
For sure. The fact is that I would like to avoid entering manual input. Just having a visual output of what is happening in the script is sufficient for my purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.