我想知道是否可以调用python解释器,使得python语句被打印(回显)到标准输出而不是静默执行。
任何想法?
答案 0 :(得分:1)
听起来你需要一个调试器。如果您对python debug或pudb感到不舒服,那么您可以尝试其中一个无数的可视化调试器。
除了在代码开头设置一个断点,然后进入"每一行,我都不知道调试器会在解释时将每个语句打印到输出。
答案 1 :(得分:1)
import sys
for _i in sys.path:
print _i
然后我可以使用命令:
$ python -m trace -t testScript.py
结果将如下所示:
--- 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)
import sys
def test():
for _i in sys.path:
print _i
然后我这样做:
$ python
>>> import imp
>>> mytest=imp.load_source('mytest','test.py')
使用跟踪模块跟踪代码:
>>> import trace
>>> tracer=trace.Trace()
>>> tracer.run('mytest.test()')
结果将是:
--- 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)