andreasdr
andreasdr

Reputation: 4054

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

Upvotes: 22

Views: 9680

Answers (6)

Elias
Elias

Reputation: 811

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:

Run ==> Edit Configurations... ==> Execution: Run with Python console (Enable)

And that's it!!

Upvotes: 2

Marky0
Marky0

Reputation: 2084

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)enter image description here

Upvotes: 4

Robert Ewing
Robert Ewing

Reputation: 1

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

Upvotes: 0

TheExorcist
TheExorcist

Reputation: 2006

I do it like this.

python -i my_fav_file.py

-i switch helps you in calling interactive python console.

enter image description here

We can do it anywhere even in pycharm terminal.

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174624

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.

Upvotes: 10

Ffisegydd
Ffisegydd

Reputation: 53678

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.

Upvotes: 7

Related Questions