6

After years of research programming in Matlab, I miss the way I could pause a program mid-execution and inspect the variables, do plotting, save/modify data, etc. via the interactive console, and then resume execution.

Is there a way to do the same thing in python?

For example:


 # ... python code ...
 RunInterpreter
 # Interactive console is displayed, so user can inspect local/global variables
 # User types CTRL-D to exit, and script then continues to run
 # ... more python code ...

This would make debugging a lot easier. Suggestions much appreciated, thanks!

asked Dec 22, 2010 at 17:18

6 Answers 6

6

Use the pdb library.

I have this line bound to <F8> in Vim:

import pdb; pdb.set_trace()

That will drop you into a pdb console.

The pdb console isn't quite the same as the standard Python console... But it will do most of the same stuff. Also, in my ~/.pdbrc, I've got:

alias i from IPython.Shell import IPShellEmbed as IPSh; IPSh(argv='')()

So that I can get into a "real" iPython shell from pdb with the i command:

(pdb) i
...
In [1]:
answered Dec 22, 2010 at 17:20
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, nice iPython! Gotta give that a try.
5

The excellent solution I found was to use the 'code' module. I can now call 'DebugKeyboard()' from anywhere in my code and the interpreter prompt will pop-up, allowing me to examine variables and run code. CTRL-D will continue the program.

import code
import sys 
def DebugKeyboard(banner="Debugger started (CTRL-D to quit)"):
 # use exception trick to pick up the current frame
 try:
 raise None
 except:
 frame = sys.exc_info()[2].tb_frame.f_back
 # evaluate commands in current namespace
 namespace = frame.f_globals.copy()
 namespace.update(frame.f_locals)
 print "START DEBUG"
 code.interact(banner=banner, local=namespace)
 print "END DEBUG"
answered May 24, 2011 at 20:35

Comments

3

The code module contains classes for bringing up a REPL.

answered Dec 22, 2010 at 17:21

1 Comment

Interesting, I did not know about it. I will give it a try.
2

Check out the Python debugger. In short, you can insert

import pdb; pdb.set_trace()

at any point in your program that you want to debug. (Note that you should remove these in release versions!)

answered Dec 22, 2010 at 17:21

Comments

1

pdb is what you're looking for - just put a call to pdb.set_trace() wherever you want to drop into an debugger.

answered Dec 22, 2010 at 17:20

Comments

-1

Here is a better, simpler solution, works in Python 3.8 https://stackoverflow.com/a/1396386/4566456

Even more powerful if IPython is installed https://stackoverflow.com/a/8152484/4566456

answered May 8, 2020 at 23:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.