I am trying to track down when a variable gets updated. I have a watcher, but is there any way to set it up so that the execution can be paused when the value is getting updated?
This is called a watchpoint or a data breakpoint.
Currently pycharm does not have a default built-in feature that tracks variable modification in real time. Alternatively you can do this:
run debug
From debugger pane -> Variables, right click the variable you would like to track and and add it to Watches.
In Watches pane, right click the variable and select referring objects.
The feature you are talking about is, I believe, called watchpoint support and according to this article:
http://sourceforge.net/blog/watchpoints-in-python/
Eric and PyScriptor has the feature but not pycharm.
Checkout watchpoints:
watchpoints is an easy-to-use, intuitive variable/object monitor tool for python that behaves similar to watchpoints in gdb.
An answer to How do you watch a variable in pdb describes how it compares to other approaches and why it's favorable.
As for better integration with pycharm, see Support other debuggers like pydevd
Regarding built-in python support and performance impact, see:
Add C hook in PyDict_SetItem for debuggers
add support for watching writes to selected dictionaries
Further notes are available on other questions on SO:
How can I make PyCharm break when a variable takes a certain value? "without modifying my code"
Is there a way to set a breakpoint on variable access in Python with PyDev? concerns a global variable
How to trigger function on value change? combining observable with breakpoint() or pydevd.settrace() might be a solution if more control is needed
Is there a free python debugger that has watchpoints? [closed, "asking us to recommend"]
Lastly, I repeat the proposal to vote for Support Data breakpoints PyCharm issue.
You can add a breakpoint in the line you need to watch and right-click it.
Then in the dialog box you have "condition" as last input: add a condition that uses the variable you need and it should stop when you set it to.
Related
In RStudio, function variables, parameters or arguments are displayed by pressing tab.
While VSCode has a lot of features, I cannot find a similar one for Python.
I found a way for VSCode to show me the definition of the function while hovering in the function itself, but there are no autocompletion for the actual variables of that function (nor suggestions while writing). Besides, the tooltips close itself as soon as I start typing the variables.
Is there a way to get something more similar regarding autocompletion and suggestion of function variables in VSCode while using Python?
Thanks.
According to your description, it is recommended that you use the extension "Pylance", which provides outstanding language service functions.
Its 'Docstrings' and 'auto-completion' functions show us the function parameters and will not close the prompt when inputting:
Part of its function introduction:
I like VS2019 and I want to do as much dev on it without needing to switch IDEs constantly. To this, I tried coding in Python but when it came to debugging, it really hold no weight to Pycharm.
For one, the “Autos” variables don’t show on my end:
This is with a project I created within VS2019. Instead to see variables, I have to go to the super cluttered “locals” tab which actually includes for whatever reason, collections, and a bunch of packages cluttering up my debug monitor. I can’t even take out these variables so I can have a cleaner window
In C++, Autos were automatically populated with variables within the scope of the current function call. In the locals everything including stuff I don’t care is there:
The worst part, making classes with multiple values, the object of the class in the debug window can’t even expan to show you the values the object holds like it does so well in Pycharm:
Is there a way to fix this? Different debug monitor windows you can use to make variable tracking as close to and intuitive as Pycharm?
I fixed it by going to Tools->Options->Python->Debugging, and changing it to "use legacy debugger". Unfortunately it doesn't track multipe variables on auto. Just the most recent one
I have started using VS with Python and I was expecting to have similar features as in R.
What I need is to be able to edit and execute line by line ("Sent to Interactive" command) as well as see current values of the defined variables?
This last item is missing and I am not able to find any way to display values of
all current variables in Local, Watch or any other window while doing interactive Edit Execute?
(There are similar question at StackOverflow but couldn't find answer to this particular one)
There isn't any obvious way to inspect variables in the current context in Interactive Window in PTVS, unfortunately. We are aware of this deficiency, and would like to fix it in future versions.
In the meantime, there are some workarounds that may be "good enough". The most obvious one is to use dir() to dump the names of the locals (and you can turn it into a one-liner dict comprehension to dump the values alongside with the names).
Alternatively, you can attach PTVS debugger to the Python process backing the Interactive by using the $attach magic command. This works the same as a regular debugging session, and so you won't see the locals until you somehow pause the process. The easiest way to do so is to cause an exception to be raised in the REPL, e.g. simply by typing raise Exception - then you'll be paused in the right frame, and can see all your variables in Locals and edit them as usual; of course, you'll have to unpause the process before you can use the REPL again.
I am trying to track down when a variable gets updated. I have a watcher, but is there any way to set it up so that the execution can be paused when the value is getting updated?
This is called a watchpoint or a data breakpoint.
Currently pycharm does not have a default built-in feature that tracks variable modification in real time. Alternatively you can do this:
run debug
From debugger pane -> Variables, right click the variable you would like to track and and add it to Watches.
In Watches pane, right click the variable and select referring objects.
The feature you are talking about is, I believe, called watchpoint support and according to this article:
http://sourceforge.net/blog/watchpoints-in-python/
Eric and PyScriptor has the feature but not pycharm.
Checkout watchpoints:
watchpoints is an easy-to-use, intuitive variable/object monitor tool for python that behaves similar to watchpoints in gdb.
An answer to How do you watch a variable in pdb describes how it compares to other approaches and why it's favorable.
As for better integration with pycharm, see Support other debuggers like pydevd
Regarding built-in python support and performance impact, see:
Add C hook in PyDict_SetItem for debuggers
add support for watching writes to selected dictionaries
Further notes are available on other questions on SO:
How can I make PyCharm break when a variable takes a certain value? "without modifying my code"
Is there a way to set a breakpoint on variable access in Python with PyDev? concerns a global variable
How to trigger function on value change? combining observable with breakpoint() or pydevd.settrace() might be a solution if more control is needed
Is there a free python debugger that has watchpoints? [closed, "asking us to recommend"]
Lastly, I repeat the proposal to vote for Support Data breakpoints PyCharm issue.
You can add a breakpoint in the line you need to watch and right-click it.
Then in the dialog box you have "condition" as last input: add a condition that uses the variable you need and it should stop when you set it to.
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.