i have a python module with a function:
我有一个功能的python模块:
def do_stuff(param1 = 'a'):
if type(param1) == int:
# enter python interpreter here
do_something()
else:
do_something_else()
is there a way to drop into the command line interpreter where i have the comment? so that if i run the following in python:
有没有办法进入命令行解释器,我有评论?所以,如果我在python中运行以下内容:
>>> import my_module
>>> do_stuff(1)
i get my next prompt in the scope and context of where i have the comment in do_stuff()
?
我在do_stuff()的评论范围和上下文中得到了我的下一个提示?
3 个解决方案
#1
47
Inserting
插入
import pdb; pdb.set_trace()
will enter the python debugger at that point
此时将进入python调试器
See here: http://docs.python.org/library/pdb.html
见这里:http://docs.python.org/library/pdb.html
#2
123
If you want a standard interactive prompt (instead of the debugger, as shown by prestomation), you can do this:
如果你想要一个标准的交互式提示(而不是调试器,如prestomation所示),你可以这样做:
import code
code.interact(local=locals())
See: the code module.
请参阅:代码模块。
If you have IPython installed, and want an IPython shell instead, you can do this for IPython >= 0.11:
如果你安装了IPython,并且想要一个IPython shell,你可以为IPython> = 0.11执行此操作:
import IPython; IPython.embed()
or for older versions:
或旧版本:
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell(local_ns=locals())
#3
18
If you want a default Python interpreter, you can do
如果你想要一个默认的Python解释器,你可以这样做
import code
code.interact(local=dict(globals(), **locals()))
This will allow access to both locals and globals.
这将允许访问本地和全局。
If you want to drop into an IPython interpreter, the IPShellEmbed
solution is outdated. Currently what works is:
如果要放入IPython解释器,IPShellEmbed解决方案已过时。目前有效的是:
from IPython import embed
embed()
#1
47
Inserting
插入
import pdb; pdb.set_trace()
will enter the python debugger at that point
此时将进入python调试器
See here: http://docs.python.org/library/pdb.html
见这里:http://docs.python.org/library/pdb.html
#2
123
If you want a standard interactive prompt (instead of the debugger, as shown by prestomation), you can do this:
如果你想要一个标准的交互式提示(而不是调试器,如prestomation所示),你可以这样做:
import code
code.interact(local=locals())
See: the code module.
请参阅:代码模块。
If you have IPython installed, and want an IPython shell instead, you can do this for IPython >= 0.11:
如果你安装了IPython,并且想要一个IPython shell,你可以为IPython> = 0.11执行此操作:
import IPython; IPython.embed()
or for older versions:
或旧版本:
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell(local_ns=locals())
#3
18
If you want a default Python interpreter, you can do
如果你想要一个默认的Python解释器,你可以这样做
import code
code.interact(local=dict(globals(), **locals()))
This will allow access to both locals and globals.
这将允许访问本地和全局。
If you want to drop into an IPython interpreter, the IPShellEmbed
solution is outdated. Currently what works is:
如果要放入IPython解释器,IPShellEmbed解决方案已过时。目前有效的是:
from IPython import embed
embed()