Enter Interactive Mode In Python

PythonInteractive

Python Problem Overview


I'm running my Python program and have a point where it would be useful to jump in and see what's going on, and then step out again. Sort of like a temporary console mode.

In Matlab, I'd use the keyboard command to do this, but I'm not sure what the command is in python.

Is there a way to do this?

For instance:

for thing in set_of_things:
    enter_interactive_mode_here()
    do_stuff_to(thing)

When enter_interactive_mode() calls, I'd like to go there, look around, and then leave and have the program continue running.

Python Solutions


Solution 1 - Python

code.interact() seems to work somehow:

>>> import code
>>> def foo():
...     a = 10
...     code.interact(local=locals())
...     return a
... 
>>> foo()
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> a
10

Ctrl+Z returns to the "main" interpreter.

You can read the locals, but modifying them doesn't seem to work this way.

Solution 2 - Python

python -i myapp.py

This will execute myapp.py and drop you in the interactive shell. From there you can execute functions and check their output, with the whole environment (imports, etc.) of myapp.py loaded.

For something more sophisticated - it would be better to use a debugger like pdb, setting a breakpoint. Also, most IDEs (PyDev, PyCharm, Komodo...) have graphical debuggers.

Solution 3 - Python

I use pdb for this purpose. I realize Emil already mentioned this in his answer, but he did not include an example or elaborate on why it answers your question.

for thing in set_of_things:
    import pdb; pdb.set_trace()
    do_stuff_to(thing)

You can read and set variables by starting your command with an exclamation point. You can also move up and down the stack (commands u and d), which InteractiveConsole does not have built-in mechanisms to do.

To have the program continue executing, use the c command. In the above example it will enter the debugger every loop iteration, so you might want to wrap the set_trace() call in an if sentence.

Solution 4 - Python

You have options -- Python standard library or IPython.

The Python standard library has a code module which has an InteractiveConsole class whose purpose is to "Closely emulate the behavior of the interactive Python interpreter." This would probably be able to do what you want, but the documentation doesn't have any examples on how to use this, and I don't have any suggestions on where to go.

IPython, which is a more advanced Python terminal, has the option to embed a console at any point in your program built in. According to their documentation, you can simply do

from IPython import embed

for thing in set_of_things:
  embed()
  do_stuff_to(thing)

Solution 5 - Python

Most comfortable tool for me is [ipdb][1]. > ipdb exports functions to access the IPython debugger, which features tab completion, syntax highlighting, better tracebacks, better introspection with the same interface as the pdb module. [1]: http://pypi.python.org/pypi/ipdb >

Completion and handy introspection is especially useful for debugging.

Solution 6 - Python

You can use ipdb.

> To set your breakpoints, add import ipdb; ipdb.set_trace() where you want to jump into the debugger. Once you reach a breakpoint, you’ll be given an interactive shell and a few lines of code around your breakpoint for context. > > https://www.safaribooksonline.com/blog/2014/11/18/intro-python-debugger/

Solution 7 - Python

From Python 3.7 onwards, you can also use breakpoint() to get into the debugger, e.g.:

for thing in set_of_things:
    breakpoint()
    do_stuff_to(thing)

This is a little easier to remember and write, and will open your code in pdb by default.

However, it's also possible to set the PYTHONBREAKPOINT environment to the name of a callable, which could be another debugger such as pudb or ipdb, or it could be IPython's embed, or anything else.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichardView Question on Stackoverflow
Solution 1 - PythonKosView Answer on Stackoverflow
Solution 2 - PythonEmil IvanovView Answer on Stackoverflow
Solution 3 - PythonLauritz V. ThaulowView Answer on Stackoverflow
Solution 4 - PythonSam MussmannView Answer on Stackoverflow
Solution 5 - Pythong1zmoView Answer on Stackoverflow
Solution 6 - PythonfredrikView Answer on Stackoverflow
Solution 7 - PythonTom CarrickView Answer on Stackoverflow