16
votes

Are there any smooth way to run Python scripts in the PyCharm's console?

My previous IDE - PyScripter - provides me with that nice little feature. As far as I know PyCharm has 2 ways of running script in console: 1) Select a bunch of code and press Ctrl+Alt+E. 2) Save the code in a file and import it from the Console.

Are the any way to do it by pressing "Run" or "Debug" buttons? I need to see the result of my script in the console and all variables available to manipulate.

6

6 Answers

28
votes

In the Run/Debug Configuration, add -i to the interpreter options. This will stop it from closing a python session even after a successful run. I.e. you will be able to see all the variable contents

3
votes

If you create run-time configuration then after pressing the run button (green "play" icon) you will get the desired result: your code will give you output in a console which opens atomatically at the bottom.

You can create many different configuraions for run and debug within a single project.

Here is a link from PyCharm's web help which covers the run/debug configurations: http://www.jetbrains.com/pycharm/webhelp/run-debug-configuration-python.html

A possible way of manipulating the variables using debug mode and evaluate expression window is added in comment but I missed one detail: to see the result of your interactive code execution you have to switch from Debugger to Console output; mode tabs are on the top-left side of the bottom pane.

2
votes

Run -> Edit configuration -> select the script you want to run and give it a display name -> OK

Now you can run it with the green "Run" button. The green bug button (next to the run button) will run it in debug mode.

Remark: next to the run button you can monitor the script/configuration you run by selecting it's display name.

1
votes

The way I do it is a create my script in the editor, call it myfirst.py, eg

print "Hello"
a= 1234

then in the console I run:

reload (myfirst)

To check on the variables use:

>>> myfirst.a

Not perfect but that's pyCharm for you. If you want a pyScripter like experience which I think is more useful you can try spyder2.

1
votes

It may also be a good option for you to use the magic command

%run scriptname.py

in the ipython console (including the %-character). Compared to Cntrl + Alt + E you also get clean information on errors in your code. Due to autocomplete it's also typed in a second and you can use this in any environment with an ipython shell. For me as a scientist who often uses python to explore data, this is a good option.

0
votes

With your program in the editor:

Run - Edit Configurations

Then under Execution check the box for "Run with Python console".

When you run the program from the green arrow it will run in a console. After the run all your objects are available to examine and play with.

enter image description here