How to get Python interactive console in current namespace?

六月ゝ 毕业季﹏ 提交于 2020-01-11 16:02:08

问题


I would like to have my Python code start a Python interactive console (REPL) in the middle of running code using something like code.interact(). But the console that code.interact() starts doesn't see the variables in the current namespace. How do I do something like:

mystring="hello"

code.interact()

... and then in the interactive console that starts, I should be able to type mystring and get "hello". Is this possible? Do I need to set the "local" argument of code.interact() to something? What would this be set to? How should it be called?


回答1:


Try:

code.interact(local=locals())

(found here: http://aymanh.com/python-debugging-techniques)




回答2:


For debug I usually use this

from pdb import set_trace; set_trace()

it may help



来源:https://stackoverflow.com/questions/7165493/how-to-get-python-interactive-console-in-current-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!