I need to run my Python script as usual, but I want to stop execution on a specific line and start interactive mode.
In other words, I want to be able to check the value of all my variables at that point, and continue myself from there on python's command line.
How can I do this?
This can be done with the code module. The easiest way is to call code.interact().
Use a debugger and add breakpoints. Do you use an IDE? All the major IDEs have debugger support. From the CLI, you can use pdb.
Not exactly what you're looking for, but you can easily have your program break out to pdb (the Python debugger) by adding this line wherever you want your program to break out:
import pdb; pdb.set_trace()
You can then easily check variables like this:
p variable_name
You can also step, continue etc.
More detail on pdb here.
Unless you need this for production purposes the best way, in my opinion, is to use interactive debugger:
http://infohost.nmt.edu/tcc/help/pubs/python/web/pdb.html
http://onlamp.com/pub/a/python/2005/09/01/debugger.html
for other purposes consider maybe doing aspects on your code, using decorators to get runtime characteristics from method class:
http://www.cs.tut.fi/~ask/aspects/index.shtml
http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch26.html
Related
I am new to Python, have some experience in MatLab and r. My question is: Is it possible to run part of the code in .py block by block (line by line)?
In r or Matlab, I can first have some data and variables loaded in the memory first. Then experimentally I can run a line or two to try out the syntax... this is particularly useful for new learners I believe. I know there is something called the iPython which can execute Python code line by line however this is not what I am after. Thanks.
Since ipython has already been discounted, I'm not sure this answer will be better. But I will tell you the two things that I do.
I drop into the debugger at the point where I want to "try out" something, so the code will run up to that point, and then drop me into the debugger. You do this simply by inserting this code at that point:
import pdb; pdb.set_trace()
Once you've done what needs to be done, you can either press q to quit, or c to continue running the process.
I use the -i option to python. This enters interactive mode at the end of your python code. This is useful if you want to set up a bunch of data structures, and try out some code on it, instead of typing all of it into a python shell first. (that might be why you rejected ipython?)
I think what you need is a debugger.
You can use the pydev plugin for Eclipse which has a debugger.
Another option is pdb as already suggested but it's not very easy to use.
I'm writing program in python in which user is to work with program by command-line. I'm using raw_input to get command from user. I want to have "memory" like in bash, etc, so, if you press an arrow (up or down) on your keyboard, you get previous/next command. I know about one way to do it (simply get every char typed by user and check it), but maybe you know something better / cuter :-)
greetings
If I understand what you want, you can achieve it simply by importing the readline module. This will modify the behavior of raw_input() so that it behaves more like the python interactive shell in terms of history and line editing.
Be careful though, it's possible to build python without readline so I'd suggest importing it inside a try block:
try:
import readline
except:
pass #readline not available
The built-in readline module provides this functionality.
I want to utilize introspection capability of python for debugging/development, but cannot find appropriate tool for this.
I need to enter into shell (IPython for example) at specific position or at specific event (like exception), with locals and globals of shell being set to the frame's ones.
My own quick hack to illustrate it:
import inspect
from IPython.Shell import IPShellEmbed
def run_debug():
stack = inspect.stack()
frame = stack[1][0]
loc = frame.f_locals
glob = frame.f_globals
shell = IPShellEmbed()
shell(local_ns=loc, global_ns=glob)
With according run_debug() call from 'breakpoint' or try/except. But, obviously, this needs alot of polishing, esp to work with threaded apps properly.
winpdb has breakpoints with console, but I found no way to quickly run proper python shell from it, and eval()/exec() are not very handy for long debug.
Similar to what you're already doing, there's ipdb. Effectively, it's pdb with ipython's shell (i.e. tab completion, all the various magic functions, etc).
It's actually doing exactly what the little code snipped you posted in your question does, but wraps it into a simple "ipdb.set_trace()" call.
For personal/education purposes you can use WingIDE - they have some pretty solid debugging capabilities.
Of course if you're just worried about changing values you can always just use raw_input() - but that may not be advanced enough for your needs.
If you run your code from ipython and hit an exception, you can call %debug afterwards to drop into a pdb at the exception. This should give you what you want. Or if you run ipython -pdb, you will automatically be dropped into pdb when an uncaught exception occurs.
I'm debugging an script that I'm writing and the result of
executing a statement from pdb does not make sense so my
natural reaction is to try to trace it with pdb.
To paraphrase:
Yo dawg, I like python, so can you put my pdb in my pdb so I can debug while I debug?
It sounds like you're looking for something listed fairly prominently in the docs, which is the set of methods that let you programmatically invoke the debugger on expressions, code in strings, or functions:
http://docs.python.org/library/pdb.html#pdb.run
http://docs.python.org/library/pdb.html#pdb.runeval
http://docs.python.org/library/pdb.html#pdb.runcall
I use these when I'm already at the pdb prompt (generally having gotten there by encountering a well-placed pdb.set_trace() statement) and want to test out, for example, variations on some method calls that aren't called in my source but which I can call right in the current context, manually.
If that's not what you were looking for, do you simply want the "step" command instead of the "next" command at the prompt? (It's unclear what you really want here. An example might help.)
I try Eclipse+PyDev pair for some of my work. (Eclipse v3.5.0 + PyDev v1.5.6) I couldn't find a way to expose all of my variables to the PyDev console (Through PyDev console -> Console for current active editor option) I use a simple code to describe the issue. When I step-by-step go through the code I can't access my "x" variable from the console. It is viewed on Variables tab, but that's not really what I want.
Any help is appreciate.
See my screenshot for better description:
EDIT:
Assume adding a simple func like:
def myfunc(x):
return x**x
When I debug with the function added in the code I can access myfunc from the console easily. (Type myfunc and it will be available after this automatic execution:
>>> from part2.test import myfunc
>>> myfunc
Then when I do myfunc(5) it acts just like in the Python interpreter. It would be so useful to access variables in the similar fashion for debugging my code. I have big arrays and I do various tests and operations during debug process. Like:
Get my x and do x.sum(), later do x[::10], or transpose operate with other arrays observe results, experiment etc...
Hope there will be a better solution.
Update:
In the latest PyDev versions, it's possible to right-click a frame in the stack and select PyDev > Debug console to have the interactive console with more functions associated to a context during a debug session.
Unfortunately, the actual interactive console, which would be the preferred way of playing with code (with code-completion, etc -- http://pydev.org/manual_adv_interactive_console.html) has no connection to a debug session right now (this is planned but still not implemented).
Still, with the 'simpler' console available, you are still able to interactively inspect and play with the variables available in a breakpoint scope: http://pydev.org/manual_adv_debug_console.html (which is the same as you'd have with pdb -- it's just a matter of typing code in the available console after a breakpoint is hit).
Cheers,
Fabio
For this sort of exploratory debugging I like to use pdb, the batteries-included debugger. I haven't used it inside PyDev so I don't know how it would all fit together. My guess is it will do what you expect it to. An example of its usage:
import pdb
def myfunc(x):
pdb.set_trace()
return x**x
This will break right before executing the return statement, and it allows you to use full Pythonic statements to figure out what's going on. I use it like an interactive print statement: setting the place where I want to dive in, examining values and figuring results, and stepping through to watch it happen. Perhaps this is a lazy way of debugging, but sometimes you need more information before you can make less-lazy decisions :-)
The page I usually reference is at Python Conquers The Universe which also links a few other sources of information.