Pass to python interpreter when 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 look into the command line interpreter where I have a comment? so if i run the following in python:

 >>> import my_module >>> do_stuff(1) 

I get the following prompt in scope and context where I have a comment in do_stuff() ?

+61
python
Jan 28
source share
3 answers

Insert

 import pdb; pdb.set_trace() 

will go into python debugger at this point

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

+40
Jan 28 '10 at 21:30
source share

If you want a standard interactive prompt (instead of a debugger, as shown in preliminary order), you can do this:

 import code code.interact(local=locals()) 

See: module code .

If you have IPython installed and require 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()) 
+109
Jan 28
source share

If you want to use the default Python interpreter, you can do

 import code code.interact(local=dict(globals(), **locals())) 

This will allow access to both local and global.

If you want to go to the IPython interpreter, the IPShellEmbed solution IPShellEmbed deprecated . Currently working:

 from IPython import embed embed() 
+13
Oct 06
source share



All Articles