drop into python interpreter while executing function

后端 未结 3 1635
耶瑟儿~
耶瑟儿~ 2020-12-07 07:39

i have a python module with a function:

def do_stuff(param1 = \'a\'):
    if type(param1) == int:
        # enter python interpreter here
        do_somethin         


        
相关标签:
3条回答
  • 2020-12-07 08:27

    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())
    
    0 讨论(0)
  • 2020-12-07 08:33

    Inserting

    import pdb; pdb.set_trace()
    

    will enter the python debugger at that point

    See here: http://docs.python.org/library/pdb.html

    0 讨论(0)
  • 2020-12-07 08:37

    If you want a default Python interpreter, you can do

    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:

    from IPython import embed
    embed()
    
    0 讨论(0)
提交回复
热议问题