I'm looking for a way to give the Python console in PyCharm (available from Tools
-> Run Python Console...
) access to the variables that were defined by a script that I'm currently working with.
For instance, say that my script (part of a PyCharm project) contains the line
aa = 4
I then want to go straight to the console and manipulate the variable that was just defined by the script, e.g.
>>> aa*2
8
I can't find a way to do this, and the related question Is there a Variable Explorer for PyCharm doesn't help me: the accepted answer there seems to imply that the console in fact should have access to the variable space of the current workspace/script, but that isn't true in my case.
(As a side note: The above was possible in the only other IDE that I've tried other than PyCharm: PyScripter. It is also how I'm used to work in MATLAB.)
Use the menu: Tools / open debug command line. There is also an icon to the left of the console when you're debugging (looks like a '>' prompt, not obvious) that triggers this. Then you have access to your variables.
PyCharm has a page on this: see https://www.jetbrains.com/help/pycharm/debug-tool-window-console.html
This answer works in the Pycharm Python console. Another option is to run in debug mode. You can click on the "Show Python Prompt" icon in the bottom left corner of the debug console to open a prompt to access the variable space (circled red in screenshot below Pycharm 2017.3.3 Community edition)
The way to do this, if you don't have ipython:
-i nameoffile.py
, wherenameoffile.py
is the name of the file you want to have available to you.Next, go to
Run > Run 'nameoffile.py'
or SHIFT+F10This will create a Python interpreter, which will already have your file's variables available. Its the "normal" way to do the
%run
magic command.You can also do this from the command line,
python -i somefile.py
will cause the Python interpreter to load with the filesomefile.py
already loaded.PyCharm can make use of an IPython console if you have it installed, what this means is that you can use the IPython magic functions such as
%run my_filename.py
to run Python code.The only way I know of doing what you want is to manually run the Python code in the console yourself, using the
%run
command, which will run the file and also give you access to any variables, functions, etc that have been defined inside your code.I do it like this.
-i switch helps you in calling interactive python console.
We can do it anywhere even in pycharm terminal.
This issue had been raised before (Access python console and program variables after program finished running? and the JetBrains group added this feature some time ago (Option in run configuration to open console after execution).
You can simply enjoy this feature which runs your .py script in Pyhton console and makes available all the created variables in console by changing Run settings in PyCharm. Precisely, you should just enable this option like bellow from the main menu:
And that's it!!