How to show variables in Python Console after running code in Pycharm? - python

I am having a problem in PyCharm (2022.3.1) that the variables of my code are not showing in the right-bottom area of "Variables" in Python Console in PyCharm.
This function is extremely useful when coding. You can keep track of the changes and data-type of your variables.
How to solve it?

The problem mentioned was solved and I will described, here, how to do it:
I ran a simple code with two variables. The code runs fine but the variables do not show (in the red circle area, as they should be).
Se this first first screenshot
If I add a new variables manually in the Python Console it will show in the variable area as it shows
So how did I solve this issue?
You will need to change the Debug configurations as shown here, and check the box "Run with Python Console", then Apply, Ok.
After that when you run your code, your variables will be shown in the variables tab at the bottom-right-corner, as shown here.
After that you will also be able to type new variables in the Python Console and they will immediately be shown in the variable tab.

Related

What happened to the Python interactive window?

I am working on MacOS v10.14.6 and using the Python interactive window of VScode v1.38.1.
I write code in a .py file and use #%% to create cells and shift+enter to run them in the interactive window.
Today the layout of the interactive window changed, but I didn't do any update and I did not change my settings.
It now shows icons on the top left (as on the screenshot below) instead of showing them on the top right, as it was the case before (as shown in the tutorial).
To me it looks like that doing shifts+enter now launchs a jupyter notebook similar to the one now supported by vscode, because the icons are similar. See the ones on this tutorial here.
The really annoying thing is that now my plots have a dark background, instead of a white one as it was the case before.
I checked and the option python.dataScience.ignoreVscodeTheme is still set on True.
Do you have an idea what happened ?
Thanks for the info Louis. So you are still seeing the interactive window there, not the new notebook experience, we just made some tweaks to the icons and icon locations to match the interactive window up better with the new experience. However while much of that is expected we did break the ignoreVscodeTheme setting. We have an issue filed on that there and it's on our immediate list to fix. Sorry about that, and thanks for reporting.
https://github.com/microsoft/vscode-python/issues/7847

How to find the variables I created in Pycharm variable explorer?

I am trying to use the variable explorer in PyCharm.
I click on python console and I have a window for the variables.
However, the variables that I created in my code do not appear, only built in variables appear. (Under special variables, there is: builtins and sys)
Where can I find the variables that appear in my code?
The specific variable I'm looking for is a dictionary called "data_model".
It is created within the program and filled during runtime.
You can run the code in python console and then you will be able to see all the variables (along side builtin variables). By default it will only show builtin and sys variables and as you run the code in python console, it will show the variables.
The dictionary view is not great I think but in pandas dataframe you will be able to right click on variable and click on 'view as a dataframe' which puts the dataframe in a new window.

How to autocomplete brackets in spyder

new to python and spyder here.
Is there a way to autocomplete brackets in spyder?!
Also is there a way to get auto indentation?
I risk making this too broad but is there also a way to make sure that if say I'm defining a function, I can simply click at the end of the function without highlighting everything to make it run.
Example:
def print_twice(Spam):
print(Spam)
print(Spam)
In the above I would like to run the last line but have spyder run everything from def print_twice(Spam) to print(Spam)
Thanks and sorry if the question is annoying or already has an answer. Couldn't find it.
(Spyder maintainer here) My answers:
Is there a way to autocomplete brackets in spyder?
Brackets are auto-completed automatically by default.
Also is there a way to get auto indentation?
Auto-indentation works automatically too, after : or by aligning your code on open parenthesis.
is there also a way to make sure that if say I'm defining a function, I can simply click at the end of the function without highlighting everything to make it run[?]
No, but you can create a cell to run your function. Cells are regions in your code delimited by comments of the form # %% and you can run them with Ctrl+Enter (run cell and stay on it) or Shift+Enter (run cell and advance to next one).

How to run only block of code in PyCharm like in Spyder?

In Spyder I can run only a part of code without running everything. I know that in PyCharm I can click right mouse button and "Execute Selection in Console", but it will be new execution without values and variables which declared before this part of code.
So, very often I need to run only few last lines of my code, with parametres and options that I already have.
you can try to use the interactive interpreter while in debug mode.
I find it to be very useful when trying to run code snippets in the program.
view the screenshot below.
You can use the "Run cell" functionality and the cell will be executed in the Python Console (the same way it does when right clicking "Execute Selection in Console"). To enable that I am aware of two options :
In settings (Ctrl+Alt+S) install the "PyCharm cell mode" plugin.
Then use ## to create code sections.
https://plugins.jetbrains.com/plugin/7858-pycharm-cell-mode
Create a new project in scientific mode (only available in PyCharm professional)
You can create code cells with #%%.
https://www.jetbrains.com/help/pycharm/matplotlib-support.html
In both cases, it creates cells that you can execute with the green "play" button like shown below :

Is there "Edit and Continue" in PyCharm? Reload code into running program like in Eclipse / PyDev?

Hi all Python developers!
In Eclipse with PyDev it is possible to edit a Python file while debugging. On save, the PyDev debugger will reload the updated code into the running program and uses my new code. How can I do the same thing in JetBrains PyCharm (using Community Edition)?
Eclipse / PyDev writes an output like this when I do that:
pydev debugger: Start reloading module: "MyWidget" ...
pydev debugger: Updated function code: <function close at 0x055F4E70>
pydev debugger: reload finished
I searched settings and web and could not find any hint. Very glad about any idea. Thx.
Edit: I found out in Eclipse/PyDev one has to be in debug mode to be able to use this feature. I tested in PyCharm, but there was no reload done.
PyCharm does not support edit and continue in either the community edition or the professional edition but here is a workaround that I have found while debugging.
Since you can run arbitrary code in the console and/or the expression evaluator, in a lot of cases, you can execute changes to the code without having to restart the application. This isn't exactly like edit-and-continue (which is a feature I really like in Visual Studio and should be part of Pycharm) but it goes a long way towards avoiding having to restart the program from scratch after a change to see if the new code works as expected.
Let me illustrate a couple of the techniques I use:
Let's say you have the following code (with a couple of typos/bugs to illustrate the techniques)
test_value = [10,9,8,7,6,55,4,3,2,1]
for i in range(0,10):
if test_value[i] == i:
print "found the value: " + i
If you run this code, first it errors because you can't print string plus integer but also I wanted to match on 5, not have 55 in the array. So here we go.
Set a break point on the for statement like this and run the code in the debugger.
When it breaks into the debugger, you realize that it should be 5 not 55. Rather than restarting, you can change line 1 to test_value = [10,9,8,7,6,5,4,3,2,1] then select the line, right click and choose Execute Line in Console... which will change the value of test_value to be the array with a 5. Now, the if statement on line 4 becomes true on the value 5. This will then trigger the syntax error on line 5.
Now if you want to make sure you have the syntax correct you can change line 5 to print "found the value: " + str(i), select the line and choose Evaluate Expression... from the right button context menu. When you click Evaluate, the result will show up either in the dialog (or in this case, since it is a print command, in the console)
Now that I've fixed these two issues, I can run the code successfully on the second pass rather than possibly multiple passes it might have taken if I didn't use these techniques. These techniques really pay off if you find a bug deep in the code where it took a while to set up.
Obviously, this is a very contrived example, but hopefully this shows how you can use both Evaluate Expression... and Execute Line in Console... to your advantage while debugging without having to restart your application each time you find a bug in the code.
Also, if you happen to be using Django, PyCharm (professional) will re-launch the server if you make changes to the code. So if you are looking at your web page and notice a problem, you can make a change to the code and switch back to the web page and as you do, either the running application or the debugged application will re-launch and the new code will be running when you refresh the page. Again, not really edit-and-continue but a pretty rapid way to make a change and test.
After all I found a useful and acceptable workaround for my question. It works in PyCharm Community Edition 3.1.2 and I assume it will do in commercial edition as well. I tested on a mid-scale project using Python 2.7.6, PySide (Qt) with one main window and 20+ widgets, tabs, whatever. Follow these steps...
Work in PyCharm on a python project :-)
Execute your code in Debug mode (did not tried Release so far)
Edit some code in one your modules imported during the life of your program
Make your program pause. To achieve this, you can click the "Pause" button of in PyCharms Debug view and then any place in your applications main window where it would need to do something (for example on a tab header). If you have a long a running task and no UI, you may place a breakpoint in a place your program often comes by.
In the Debug view, switch to the Console tab. There is a button on the left Show command line. Click this.
In the console, type in reload(MyModifiedModule) if this call fails, write import MyModifiedModule and try again.
Click resume in PyCharm.
Try the code you fixed.
There are some restrictions on this... It won't fix changes in your main method or main window, cause it won't be created again. In my tests I could not reload widgets from Qt. But it worked for classes like data containers or workers.
May the force be with you as you try this and do not hesitate to add your experiences.
I have the commercial version of PyCharm and just tried testing a simple python script. The script is the following:
for i in range(0,100):
print i
I ran the code in debug mode and placed a break point at the "print i" statement. When the debugger stopped during the first iteration I changed the code to look like this:
for i in range(0,100):
print i
print 'hello'
PyCharm did not reload/re-compile the altered script. Given this simple test my best guess would be that PyCharm does not dynamically reload .py files.
You can add hot reloading features by installing Reloadium plugin.
https://plugins.jetbrains.com/plugin/18509-reloadium
Example use (gif)
It also works without pycharm.
More details:
https://github.com/reloadware/reloadium

Categories

Resources