Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

drop into python interpreter while executing function

i have a python module with a function:

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:

>>> 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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want a standard interactive prompt (instead of the debugger, as shown by prestomation), you can do this:

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:

import IPython; IPython.embed()

or for older versions:

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell(local_ns=locals())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...