Janis Lesinskis' Blog

Assorted ramblings

  • All entries
  • About me
  • Projects
  • Economics
  • Misc
  • Software-engineering
  • Sports

Different ways to drop into an interactive python shell


When you are developing in Python it can be handy to drop into an interactive Read Evaluate Print Loop (REPL) shell. Being able to drop into an interactive shell is an important debugging tool that will be extremely useful for finding the cause, and hence fixing, some of the harder to debug issues.

There's a variety of ways in which you can work with an interactive shell with your Python code:

Interactive mode

Perhaps the simplest way to do this is to you the -i command line switch to do this.

For example:

python -i my_script.py

This will execute my_script.py then drop into an interactive Python REPL shell at the end of that scripts execution.

This is a nice way to do this from the command line but there's other options for invoking this from code:

Code module

The code module from the standard library gives us the tools to build applications which provide an interactive interpreter prompt. Specifically code.interact gives us a convenient way to drop into an interactive shell:

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

Note that code.interact is not the actual main Python interpreter but is a class that very closely emulates it (and the interact documentation states this). This is why we have to pass in explicitly the local variables that will be in scope via locals=locals() (see the documentation).

PDB

The Python Debugger, PDB can give you a REPL like shell to interact with the code. To invoke this create a breakpoint like so:

import pdb
pdb.set_trace()

This will then pop up the debugger shell at this line where set_trace() is called.

Note that unlike code.interact this does access the exact variables that are currently in scope.

There's also an argument to set_trace called header that allows you to print out a header message when the interactive debugger is started (pdb.set_trace documentation). This can sometimes be useful for sharing additional information.

Other PDB related tools

PDB is especially useful and since its in the standard library you'll always have it when you need it, but sometimes you want/need more.

If you found PDB useful but wanted some more power with postmortem debugging or wanted syntax highlighted output you may find the PDB++ package to be of interest.

There's a variety of situations in which a debugging via dropping into a local interactive shell might not be possible, for example on a headless system you won't be able to SSH in and drop into such a shell. If you are working with tools like Celery where the worker processes aren't local you might find python-remote-pdb, a remote debugger equivalent of PDB useful.

IPython

If you are using IPython you have a few other options:

import IPython
IPython.embed()

This uses IPython.terminal.embed to embed an IPython shell into your script.

Other debugging tools

If you have some other situation you may find the list of suggestions over on the Python.org wiki useful: https://wiki.python.org/moin/PythonDebuggingTools

Published: Sun 21 July 2019
Modified: Wed 31 July 2019
By Janis Lesinskis
In Software-engineering
Tags: python software-engineering debugging

links

  • JaggedVerge

social

  • My GitHub page
  • LinkedIn

Proudly powered by Pelican, which takes great advantage of Python.