How to give the Python console in PyCharm access to the variable space of a script?
The way to do this, if you don't have ipython:
- Go to Run > Edit Configurations
- In the interpreter options dialog, type
-i nameoffile.py
, wherenameoffile.py
is the name of the file you want to have available to you. - Click Apply, then OK.
Next, go to Run > Run 'nameoffile.py'
or SHIFT+F10
This 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 file somefile.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.
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)