No Auto Variables Debugging Python in VS2019 - python

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

Related

How to group/collect variables and keep IDE (PyCharm) highlighting?

In Python, I often collect several variables that I use as parameters or settings into one container.
For instance, I often organize settings (or parameters or options) like
nr_of_users = 100
file_label = "copy"
into a dictionary, like so
options['nr_of_users']= 100
options['file_label']= "copy"
This makes it easy to print & review these parameters, and pass them between functions in chunks etc..., since they are all collected under one "handle".
And I can add fields as they come-up, without first defining a class and its members.
The issue with this approach is that my IDE doesn't recognize these as variables, and I miss out on variable highlighting (linking definition to use), and refactoring (renaming) support.
(My IDE is PyCharm, but I suspect it's not unique to this one)
Is there a better approach?

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.

Suspend debugger when a variable value changes [duplicate]

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.

Stop at the line where a variable gets changed

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.

IronPython - How to prevent CLR (and other modules) from being imported

I'm setting up a web application to use IronPython for scripting various user actions and I'll be exposing various business objects ready for accessing by the script. I want to make it impossible for the user to import the CLR or other assemblies in order to keep the script's capabilities simple and restricted to the functionality I expose in my business objects.
How do I prevent the CLR and other assemblies/modules from being imported?
This would prevent imports of both python modules and .Net objects so may not be what you want. (I'm relatively new to Python so I might be missing some things as well):
Setup the environment.
Import anything you need the user to have access to.
Either prepend to their script or execute:
__builtins__.__import__ = None #Stops imports working
reload = None #Stops reloading working (specifically stops them reloading builtins
#giving back an unbroken __import___!
then execute their script.
You'll have to search the script for the imports you don't want them to use, and reject the script in toto if it contains any of them.
Basically, just reject the script if it contains Assembly.Load, import or AddReference.
You might want to implement the protection using Microsoft's Code Access Security. I myself am not fully aware of its workings (or how to make it work with IPy), but its something which I feel you should consider.
There's a discussion thread on the IPy mailing list which you might want to look at. The question asked is similar to yours.
If you'd like to disable certain built-in modules I'd suggest filing a feature request over at ironpython.codeplex.com. This should be an easy enough thing to implement.
Otherwise you could simply look at either Importer.cs and disallow the import there or you could simply delete ClrModule.cs from IronPython and re-build (and potentially remove any references to it).
In case anyone comes across this thread from google still (like i did)
I managed to disable 'import clr' in python scripts by commenting out the line
//[assembly: PythonModule("clr", typeof(IronPython.Runtime.ClrModule))]
in ClrModule.cs, but i'm not convinced this is a full solution to preventing unwanted access, since you will still need to override things like the file builtin.

Categories