Python debugging in Eclipse+PyDev - python

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.

Related

any Python IDE supports stopping in breakpoint from the debugger

I am looking for the following (IMHO, very important) feature:
Suppose I have two functions fa() and fb(), both of them has a breakpoint.
I am now stopped in the breakpoint in fa function.
In the interactive debugger console I am calling fb().
I want to stop in fb breakpoint, but, unfortunately pb() runs but ignores the breakpoint.
someone in another SO thread called it "nested breakpoints".
I am a developer that come from Matlab, in Matlab no matter how a function is called, from console, from debugger. if it has a breakpoint it stops.
I read past threads about this subject and did not find any solution.
I also tried latest pycharm community and latest pydev and no luck.
I also read that visual studio can not make it.
Is this inherent in Python and technically can not be done?
Is there a technique / another IDE that supports it?
I followed this question and saw nobody is answering, I thought about this feature too so I started digging through google and found your old question :)
TL;DR - You can't do that
(I tried with PyCharm, Visual-Studio and Eric Python IDE).
My guess that not working because it adds more complexity to debugging - what happened when you step to the next line? and if you have many threads/processes? what happens to mutable types?
My way to do it
If you have 2 functions and you want to debug both of them:
def parent():
dummy_debuggable_var = 1
print('Running child() function')
out = child(dummy_debuggable_var) #BP1 is here
print(out)
def child(x):
print('Calculating ...')
return x+2
And your goal is to debug the dummy_debuggable_var and also the child function:
Put the break point where is comment is
Run the script
When the break point stops, inspect your dummy_debuggable_var
step-into (on PyCharm - F7)
Inspect your child function
While you're inside the child function, you can look on your PyCharm's frames stack tab on your debugger window, this will let you jump back to the parent's frame and inspect it.
This is more sequential process, and not parallel, but it's simple and it works.
Take a look at Eric Python IDE and VSC(Visual Studio Code)

How to debug PyQt based applications in Spyder

I'm doing experience with my first small applications, essentially data viewers based on Pandas and matplotlib, using PyQt for the GUI part.
What I find now difficult is to understand what goes wrong in my code, because the error does not get propagated to the iPython console I launch my script from.
It simply won't do what's expected, but there is no information as to 'why'.
To fix ideas, let's say I have a button that should plot a certain curve to the canvas. If there's a fail in the indexing operation of the data, therefore nothing can be plotted, then nothing will appear on the canvas, but I'll get no traceback that actually index so-and-so wasn't to be found.
Using the debugger proves quite cumbersome, too.
I had a situation where, while running my main(), I could interact with the IPython shell and do things like:
main.my_plot_function()
from which I would get a standard output, and see what is wrong. Although sub-optimal, this did the trick.
I had to reset Spyder this morning (wasn't launching on Windows), and since then, when I launch my script, the console is unresponsive. So I can't do `main.my_plot_function()' anymore.
Generally speaking, is there a way to instruct Spyder or the console that I want to see what's going on in the background? Some "verbose" switch?
I am not sure what you mean by wanting to know what's going on in the background. I assume that you wish to know at many points in the code, what the variable types and values are and/or where the current point of execution is.
There are two options:
1) Use print statements wherever you need to know what's going on. For example, if you have a plot function, simply put some print statements inside the function to print out the sizes of the lists/arrays being plotted etc. You can also look for useful functions in this regard, i.e., type() to print out the type of a variable to make sure it is what you think it is, print(locals()) to print names and values of all local variables etc.
2) Use pdb to introduce break points and run your main script from the command line. This will stop the script execution where you want and from the pdb console, you can inspect the data-structures. There are of course other debuggers you can use, such as pudb (with a basic GUI and some extra features than pdb).
There is no general "verbose" mode in spyder or any other Python IDE I know of.

PyCharm code completion works in console but not editor

When typing the following code into the editor window, only some of the available items for autocomplete show up. That is to say that it should show .loc as an option but doesn't.
import pandas as pd
df = pd.read_csv('somecsvfile.csv')
df.
code completion in editor window
When using the console in PyCharm with the same code, the full list shows up. (See the attached images)
code completion with the full list
I have invalidated caches and restarted. Further, it seems like another recommendation was to turn on Python Debugger -> Collect run-time types information for code insight. I did that as well and still nothing when in the editor window.
What really confuses me is that the code completion works in the console, but not the editor.
Any help would be greatly appreciated!
When you run it in the console it knows the type of df because it actually has it right there. It can even run dir(df) to know exactly what names are available. In the editor it isn't running the code so it has to guess the type by inspecting pd.read_csv which is much harder (often even impossible) because Python is so dynamic.
I used to have this same problem. This was only happening for me in Linux. Note that this is possible and actually standard behavior in windows, so it can be done. Not sure if it is done using static analysis or a similar method.
I have since been able to fix it, I think what did it was defining the correct interpreter not only in the running/debugging configuration, but also in the defaults of the project (check File-->settings-->Project Interpreter and File-->default settings-->Project Interpreter)
I have now moved to the next problem, which is that auto-complete works for Python Console and File editing, but weirdly enough does not work for Debugging!...

What is best way for interactive debug in python?

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.

Running pdb from within pdb

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.)

Categories