16

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.

mx0
  • 6,445
  • 12
  • 49
  • 54
night_bat
  • 3,212
  • 5
  • 16
  • 19

6 Answers6

28

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

Patrick
  • 281
  • 2
  • 3
3

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.

zvisofer
  • 1,346
  • 18
  • 41
  • 5
    PyCharm created new tab with 'Process finished with exit code 0' text. Nothing like Python Console, no varables avaible to process. – night_bat Feb 02 '14 at 21:09
3

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.

Mr. Girgitt
  • 2,853
  • 1
  • 19
  • 22
  • I need more than the result in a new console. I need to work with my variables from that script once script is finished – night_bat Feb 02 '14 at 21:28
  • If you set a break point and run in debug PyCharm will stop at your breakpoint where you can open "evaluate expression" window (first icon from right in the debugger pane) where you can interact with variables. If certain piece of code is worth keeping you can drag it to your code area. – Mr. Girgitt Feb 02 '14 at 21:30
1

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.

rhody
  • 2,274
  • 2
  • 22
  • 40
1

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.

Ansgar
  • 11
  • 2
0

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

Tim
  • 1,108
  • 13
  • 25