I have a python function that I'm calling from inside an iPython session.
In a very specific situation, in which a conditional in a certain line comes out as True, the script consistently drops into a pdb debug mode.
There is no trace or any other indication of a problem with the code, and as soon as I type c to continue, the code continues perfectly well.
The script doesn't include any import pdb not to mention a set_trace()...
Any ideas what could account for this?
Depending on your ipython config it automatically goes into PDB if an exception is raised.
Seems like there was a import pdb; pdb.set_trace() line in the code after all, which I missed due to source control issues.
Related
I have a python doit script that is getting stuck on one step but doesn't throw an error. It would sit all day if I let it. I've checked all the inputs and they look exactly the same as the last time I ran it. How do I debug? I tried using pdb but maybe I don't know how to use it and I googled and couldn't find example code. I can't post my code since its confidential. Just a general how to debug in doit would help me greatly. I use Python 2.7 and yes eventually I'll have to update to 3 but for now I'm using 2.7. (Sorry I have had quite a few ask why I continue with 2.7--- no time right now to update all my scripts, there are over 200)
https://pydoit.org/tools.html#set-trace
doit provides a set_trace() function that will call PDB set_trace and make sure stdout output is printed on terminal.
Not your case, but doit also provides a command line option --pdb that automatically drops in PDB when an unhandled exception occurs.
I'd like to run some Python code in debugger mode in PyCharm. My code includes an API function call, and for some reason, that single function call takes forever in debugger mode.
I really do not care about debugging that specific function, and having debugger skip over that function (only run it in regular mode) is fine. However, I'd like to be able run the rest of my code in debug mode.
Is this doable in PyCharm or is there any Python workaround?
# some code to be run in debugger mode, e.g.
func_a(obj_a) #this function modifies obj_a
# some API function call, super slow in debugger mode. can I just run this part in run mode? e.g.
obj_b = api_func(obj_a)
# rest of the code to be run in debugger mode e.g.
func_c(obj_b)
Potentially you could use sys.gettrace and sys.settrace to remove the debugger while your API call runs, though it's not recommended, and PyCharm will complain at you if you do:
PYDEV DEBUGGER WARNING:
sys.settrace() should not be used when the debugger is being used.
This may cause the debugger to stop working correctly.
If this is needed, please check:
http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html
to see how to restore the debug tracing back correctly.
In your case, you'd do something like this:
import sys
# some code to be run in debugger mode, e.g.
func_a(obj_a) #this function modifies obj_a
# Remove the trace function (but keep a reference to it).
_trace_func = sys.gettrace()
sys.settrace(None)
# some API function call, super slow in debugger mode. can I just run this part in run mode? e.g.
obj_b = api_func(obj_a)
# Put the trace function back.
sys.settrace(_trace_func)
# rest of the code to be run in debugger mode e.g.
func_c(obj_b)
I would strongly recommend keeping the code you run while the debugger is disabled as short as possible.
You can right-click on a breakpoint and set condition
I am using python's gevent library and do not want raw_input (or more specifically the event loop in cmd.Cmd) to block when awaiting user input. So as a result I use gevent.monkey.patch_sys() to ensure that my other greenlets may run when waiting for user input. Works great except that it seems to interact with readline.
For example, I no longer have history and auto-complete in ipython's ipdb debugger since the arrow keys no longer work. This can be seen with this simple snippet:
from gevent import monkey
monkey.patch_sys()
import ipdb; ipdb.set_trace()
# now hit arrow keys at the prompt
I get the following:
ipdb> ^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A
*** SyntaxError: invalid syntax (<stdin>, line 1)
I have traced it to what I think could be an issue with python's readline as I know that ipython depends on it.
Also this appears to be a similar situation:
https://github.com/gevent/gevent/issues/6
but in my case I want to specifically use auto completion and history in the debugger.
I am running on OSX within iterm. Is this something specific to a console setting? Or is something in the patch fundamentally breaking readline?
Any ideas on how to resolve?
I have a long running Python program that raises exception at some point. Is there some way to run this from ipython session and stop on the exception so I could examine the live data?
You may want ipython -i yourscript.py, which will execute your script in the interpreter environment. But this won't let you inspect the local environment where the exception happened, for example local variables within a function – you'll just be able to inspect globals. You probably want this instead:
In [1]: %run test.py
<exception occurs>
In [2]: %debug test.py
If you're not familiar with using PDB, check out some docs first.
Edit thanks to Thomas K
yes, depending on how you are setup. you can import your program and run it like any other module inside a try except block.
import yourprogram
try:
yourprogram.main_function(args)
except:
print "we blew up, investigate why"
If your program is not in a function you may need to put the try block around your import.
The problem with this approach is that the variables you are wanting to look at may be no longer in scope. I usually use print statements or log messages at various points to figure out what is not looking like I am expecting.
A fairly large Python program I write, runs, but sometimes, after running for minutes or hours, in a non easily reproducible moment, hangs and outputs nothing to the screen.
I have no idea what it is doing at that moment, and in what part of code it is.
How can I run this in a debugger or something to see what lines of code is the program executing in the moment it hangs?
Its too large to put "print" statements all over the place.
I did:
python -m trace --trace /usr/local/bin/my_program.py
but that gives me so much output that I can't really see anything, just millions of lines scrolling on the screen.
Best would be if I could send some signal to the program with "kill -SIGUSR1" or something, and at that moment the program would drop into a debugger and show me the line it stopped at and possibly allow me to step through the program then.
I've tried:
pdb usr/local/bin/my_program.py
and then:
(Pdb) cont
but what do I do to see where I am when it hangs?
It doesn't throw and exception, just seems like it waits for something, possibly in an infinite loop.
One more detail: when the program hangs, and I press ^C and then (not sure if that is necessary) the program continues normally (without throwing any exception and without giving me any hint on the screen why did it stop).
This could be useful to you. I usually do
>>> import pdb
>>> import program2debug
>>> pdb.run('program2debug.test()')
I usually add a -v option to my programs, which enables tons of print statements explaining what I'm doing in detail. When you write a program in the future, consider doing the same before it gets thousands of lines big.
You could try running it in debug mode in an IDE like pydev (eclipse) or pycharm. You can break the program at any moment and get to its current execution point.
No program is ever too big to put print statements all over the place. You need to read up on the logging module and insert lots of logging.debug() statements. This is just a better form of print statement that outputs to a file, and can be turned off easily in production software. But years from now, when you need to modify the code, you can easily turn it all back on and get the benefit of the insight of the original programmer.