如何在当前命名空间中获取Python交互式控制台? [英] How to get Python interactive console in current namespace?

查看:144
本文介绍了如何在当前命名空间中获取Python交互式控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的Python代码在运行代码中使用类似code.interact()的代码启动一个Python交互式控制台(REPL)。但是code.interact()启动的控制台看不到当前命名空间中的变量。如何执行类似的操作:



mystring =hello



code.interact >

...然后在启动的交互式控制台中,我应该能够键入mystring并获取hello。这可能吗?我需要设置code.interact()的local参数的东西吗?这将设置什么?

解决方案

尝试:

  code.interact(local = locals())

http://aymanh.com/python-debugging-techniques


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?

解决方案

Try:

code.interact(local=locals())

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

这篇关于如何在当前命名空间中获取Python交互式控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆