Variables handling Python - python

I used to code in Matlab in which it's easy to see the saved variables in the workspace window. So for example a = [1,2,3] can be easily found in the workspace window.
Now I started to use python in Pycharm. In Python I can create the same variable, a = [1,2,3] but there isn't a window which shows me all the variables already created in the script. Having only 1 variable isn't a problem but when defining a lot of variables it can be difficult to know which variables are already defined ant what these variables contain.
My Question:
How does one have a good overview of all variables in Python (Pycharm)? Is there a similar window like the Workspace window of Matlab in Pycharm?

You can use the Debug Tool Window to examine the variables stored in your application.
More information can be found here: (PyCharm 2016.3)
https://www.jetbrains.com/help/pycharm/2016.3/debug-tool-window-variables.html

Related

Function arguments suggestion/autocomplete in VSCode for Python

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:

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.

Make global variables available in multiple modules

I am creating an application consisting of several modules. There is one main.py file which will be the file to run the application. The main.py file will load the configuration file(s) and put them in the 'config'-variable. It will also import the application-module-file (the file which holds the source-code of the application itself, a.k.a. application-class) and start the instance.
I am not very experienced in coding Python, and my biggest question is if I am doing it the right way, by using a main-file to handle all needed stuff (loading configuration-files for example). The problem I am having right now is that I cannot access the 'config'-variable that was defined in the main.py-file from any other module and/or Python-file.
Is it possible to make a global variable for configuration-values exc.? I know in PHP I used to create a singleton object which holds all the specific global arguments. I could also create a global 'ROOT'-variable to hold the full path to the root of the application, which is needed to load/import new files, this is also not possible in Python as far as I know.
I hope someone can help me out of this or send me in the right direction so I can continue working on this project.
The answer seems to be by Matthias:
Use from AppName.modules import settings and then access the data in the module with settings.value. According to PEP-8, the style guide for Python code, wildcard imports should be avoided and would in fact lead to undesirable behaviour in this case.
Thanks you all for the help!

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.

Numpy/IPython equivalent to Matlab's assignin

I'm in the process of moving some of our internal data visualization/analysis tools from Matlab to IPython/Numpy/Scipy/PyLab. The tools I refer to are Matlab GUIs which do only simple kinds of analysis. Occasionally, I want to do more sophisticated analysis in which case I press a button in the GUI which executes Matlab's "assignin('base',...". This puts the data into the top-level session where I can do further operations on it.
This isn't my only use case for "assignin". As another example, I've also used it to export data for analysis to the main Matlab session from a function deep within some script. (This is just a temporary/debugging trick rather than a routine practice.)
So I ask: Is there an equivalent to Matlab's assignin when working in an interactive IPython/PyLab environment? If there is no direct equivalent, how can I go about getting data from a script launched from IPython back into the main session?
Take a look at the global namespace. Practically, I'd use something like following code, depending on whether your variable has a static name or the name can change dynamically.
def staticAssignInBase():
global x
x = 'someValue'
def dynamicAssignInBase():
g = globals()
g['someVarName'] = 'someValue'
But do note that this might not work across package boundaries!

Categories