Access python console and program variables after program finished running?

Completed
I am considering PyCharm as my main Python IDE, but I can't seem to find a way to do what is my favorite feature of Spyder. After running a program everything is just available in the python console. I can type in any variable or object and tweak it or run other code on it. However in PyCharm, it seems like once you get the "Process finished with exit code 0", the console is shutdown with no more access to any of the code run. Is there some way to keep the console alive with all that information? I'm not a fan of needing to create an extra breakpoint at the end of the program and debug every time I want to have access to anything in the code.

Also when going to tools/run python console, this console is disconnected from what is being run. I would really just like a persistent console that whatever program I run runs in. Is there a way to make it when I run the program it shows up in that console instead of making its own separate one?
5
7 comments
Avatar
Permanently deleted user
Hi, unfortunately there is no way yet to stop after script execution with the access to variables. Please vote for the feature: http://youtrack.jetbrains.com/issue/PY-11430
As for console during debugging, you can press 'Show command line' button in debugger console output tab: http://www.jetbrains.com/pycharm/webhelp/debug-tool-window-console.html
0
Avatar
Permanently deleted user

I ended up here searching for this feature. It has been implemented and it's under Run > Edit configurations > Run with Python console

Great job devs :)

8

This doesn't work if you amend the code. 

 

Is there a way to run the first part of the code in PyCharm and store it in memory and then keep working on and testing/rerunning another part of the code without a need of returning the first part each time?

This will be similar to running code in Jupiter Notebook where you can split code into multiple cells. In this example you will run a first cell (which already has been tested and which is loading data and performing complex calculations which requires time) and keep working on and re-running code in second cell to see results. Here second cell is using variables computed in the first cell, but it doesn't need to rerun the first cell - variables from the first cell are keept in memory for the whole time.

3

@k0rnik  has described a super useful feature of Spyder/Jupyter Notebooks that Pycharm needs.

I was transitioning to Pycharm but I guess it is back to Spyder for me.

1

How are you supposed to efficiently debug if you can't interact with the variables? This seems crazy to me trying to switch from JupyterLabs. Seems like it would be relatively easy to save whatever namespace the breakpoint is in and make it available in the console?

0

Has something been implemented to resolve this? I saw someone said it has been implemented and should work when ‘Run with Python Console’ is checked in the Run/Debug Configurations but this does not seem to work for me and I'm not sure if there's anything else I could try to resolve the issue as I'm using python for scientific research and having this option is quite crucial. 

I'm working on macOS and using the following version of PyCharm: 

PyCharm 2022.2.2 (Professional Edition)
Build #PY-222.4167.33, built on September 15, 2022
Runtime version: 17.0.4+7-b469.53 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 14.1.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 10

 

 

0
Yes, when "Run with python console" is enabled, you should be able to continue working in Python console and have the variables from the last run available. I'm testing on PyCharm 2023.3.3 though, so I'd suggest trying that version as well.
0

Please sign in to leave a comment.