如何在当前命名空间中获取Python交互式控制台?

use*_*702 12 python variables console namespaces read-eval-print-loop

我希望我的Python代码在运行代码的过程中使用code.interact()之类的东西启动Python交互式控制台(REPL).但是code.interact()启动的控制台没有看到当前命名空间中的变量.我该怎么做:

了mystring = "你好"

code.interact()

...然后在启动的交互式控制台中,我应该能够输入mystring并获得"hello".这可能吗?我需要将code.interact()的"local"参数设置为什么?这会是什么?该怎么称呼它?

mor*_*sik 20

尝试:

code.interact(local=locals())
Run Code Online (Sandbox Code Playgroud)

(在这里找到:http://aymanh.com/python-debugging-techniques)

  • 如果你想要全局变量也使用以下代码:code.interact(local = dict(globals(),**locals())) (19认同)