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.
Related
I am trying to find a way that I can have a program step through Python code line by line and do something with the results of each line. In effect a debugger that could be controlled programmatically rather than manually. pdb would be exactly what I am looking for if it returned its output after each step as a string and I could then call pdb again to pickup where I left off. However, instead it outputs to stdout and I have to manually input "step" via the keyboard.
Things I have tried:
I am able to redirect pdb's stdout. I could redirect it to a second
Python program which would then process it. However, I cannot
figure out how to have the second Python program tell pdb to
step.
Related to the previous one, if I could get pdb to step all the way
through to the end (perhaps I could figure out something to spoof a
keyboard repeatedly entering "step"?) and redirect the output to a
file, I could then write another program that acted like it was
stepping through the program when it was actually just reading the
file line by line.
I could use exec to manually run lines of Python code. However,
since I would be looking at one line at a time, I would need to
manually detect and handle things like conditionals, loops, and
function calls which quickly gets very complicated.
I read some posts that say that pdb is implemented using
sys.settrace. If nothing else works I should be able to recreate
the behavior I need using this.
Is there any established/straight forward way to implement the behavior that I am looking for?
sys.settrace() is the fundamental building block for stepping through Python code. pdb is implemented entirely in Python, so you can just look at the module to see how it does things. It also has various public functions/methods for stepping under program control, read the library reference for your version of Python for details.
I read some posts that say that pdb is implemented using sys.settrace.
If nothing else works I should be able to recreate the behavior I need
using this.
Don't view this as a last resort. I think it's the best approach for what you want to accomplish.
I am trying to hot swap a file in python. I am creating a game that takes a really long time to load. But I don't want to reload it every time. I am trying to change some code while the programme is in runtime.
For example:
I want to change this:
while True:
print("Hello")
to this while in runtime:
while True:
print("Hello World")
I looked hot swapping up for python and all of them are answers that I am not looking for. All the other answers change modules. I want to change the current file. Like java in eclipse. Please help!
I want to change the current file. Like java in eclipse.
When you modify Java code in Eclipse, the code is not currently running. In reality, Eclipse has a Java compiler built-in, which attempts to compile your code as you type it. That's how Eclipse is able to give you such fast feedback about syntax errors and type errors in your code. But it can't give you any information about the runtime behaviour of your code (whether or not it produces the right answer), because it doesn't run the code! You need to press the Run button for that.
So I think the question you really want to ask is not "Can I dynamically modify Python code?" (the answer to that is "Yes, but it's complicated and has caveats and is not a good idea") but "Does there exist a Python IDE which can give me feedback about syntax errors while I type?" The answer to that is an emphatic "Yes!". There's an extremely comprehensive list of options in this answer.
You can use dynamic execution using exec function.
Store the code you want to execute in a string and change it during runtime.
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
I'm experimenting with debugging my python from the raw pdb program rather than running pdb though emacs (which tracks the current line with a marker in the text display of the code). It's slightly annoying that the list command l in pdb only displays a few lines of code, I would rather have it fill my terminal with all the code of the current function up to the current line.
I know I can do this manually by looking at the line numbers are typing l 50,100 (where 100 is the current line) but this is time consuming and I'd like to set this up to work automatically.
I wonder if there is a way to define a pdb command to do this? I'm guessing it would need to (1) access the number of the current line, N; then (2) execute "l N-50, N". I've searched around a lot but can't find anyone who has done this before. Perhaps there is a way to access the pdb module's own internals to get the line number?
(Or a roundabout way would be to write something that calls list once, parses the output to extract the current line, then executes a new list command, I wonder if anyone has done this already? Is this how IDEs manage to get the current line information from pdb or are they using its internals I wonder?)
You could probably extend the PDB class, but the extension interface isn't documented very well.
I'd instead recommend using pdb++ and its sticky feature which pretty much does what you want, if I understood your use case correctly.
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.)