102

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

1

4 Answers 4

162

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())
Sign up to request clarification or add additional context in comments.

7 Comments

for IPython>=0.11, there's no more module Shell in IPython...so start it using "import IPython; IPython.embed()" instead.
Is it possible to continue after entering code.interact(), a la PDB c(ontinue)?
code.interact() is a blocking call. Your program will stop and wait for it to finish. If you exit the interactive interpreter, your program should resume with the statement immediately following.
What's the difference between an interactive prompt and a debugger?
@StockB It's the standard interactive prompt that makes it important. The debugger is an interactive prompt, but it's not the same. In PDB, you cannot do multi-line statements, the built-in help function is overridden, etc.
|
69

Inserting

import pdb; pdb.set_trace()

will enter the python debugger at that point

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

6 Comments

pdb is great vanilla python; if you have room for bringing in an external package, ipdb is great -- same functionality as the debugger, but with the syntax highlighting, tab completion, etc of ipython
Update — ipdb was deprecated, nowadays I use pdbpp (pdb++), which has similar features, and works with import pdb; pdb.set_trace() (i.e. it patches that import, so it is a drop-in replacement)
@hangtwenty: why do you say ipdb was deprecated? I can't find any news of that.
@Kundor huh, you're right. I distinctly remember reading somewhere that the authors had decided to stop maintaining the project and pointed to another project to use instead. Maybe that happened for a bit and then it was resurrected? I could be wrong! In either case, I enjoyed ipdb before, but have enjoyed pdbpp since
@Matyas pdb lets you prefix python statements/expressions with ! to avoid name clashes with pdb commands. So !list([1, 2]) will work as you expect. To enter multiline statements, you can use pdb's interact command, which is equivalent to code.interact(local={**globals(), **locals()}).
|
36

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

2 Comments

thanks, code.interact(local=dict(globals(), **locals())) is so much better than code.interact(local=locals()) because the latter makes you re-import packages
This answer worked better for me since I was using a bash script to call my python code. I could not interact with my python code using the accepted answer. Thanks!
2

Update for 2023: breakpoint() is included since py3.7 and will drop you into the debugger (pdb by default).

If you have other debuggers installed (ipdb, pdbpp, ... to name a few) then breakpoint() will instead drop you into those.

Please see these docs for more info: https://docs.python.org/3/library/pdb.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.