Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give the Python console in PyCharm access to the variable space of a script?

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.)

like image 438
andreasdr Avatar asked Oct 14 '14 07:10

andreasdr


People also ask

How do I use Python console in PyCharm?

You can assign a shortcut to open Python console: press Ctrl+Alt+S , navigate to Keymap, specify a shortcut for Main menu | Tools | Python or Debug Console. The main reason for using the Python console within PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes.

How do I inspect a variable in PyCharm?

PyCharm allows you to inspect variables in a dedicated dialog. This is useful when you need to keep track of some variable (or the object whose reference it holds) and at the same time be able to navigate between frames and threads. Right-click a variable on the Variables tab and select Inspect.

How do I set environment variables in PyCharm terminal?

Go to the Edit Configurations as pointed by other answers. Click edit the configuration templates in the panel. Edit environment variables there.


2 Answers

The way to do this, if you don't have ipython:

  1. Go to Run > Edit Configurations
  2. In the interpreter options dialog, type -i nameoffile.py, where nameoffile.py is the name of the file you want to have available to you.
  3. 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.

like image 109
Burhan Khalid Avatar answered Oct 19 '22 18:10

Burhan Khalid


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.

like image 7
Ffisegydd Avatar answered Oct 19 '22 20:10

Ffisegydd