Running code in PyCharm's console
Asked Answered
G

6

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.

Geomorphology answered 2/2, 2014 at 20:48 Comment(0)
B
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

Bruton answered 13/3, 2014 at 18:13 Comment(0)
B
4

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.

Bozeman answered 2/2, 2014 at 20:53 Comment(1)
PyCharm created new tab with 'Process finished with exit code 0' text. Nothing like Python Console, no varables avaible to process.Geomorphology
M
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.

Maurer answered 2/2, 2014 at 20:54 Comment(2)
I need more than the result in a new console. I need to work with my variables from that script once script is finishedGeomorphology
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.Maurer
P
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.

Predesignate answered 28/4, 2015 at 23:19 Comment(0)
B
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.

Blastula answered 2/3, 2018 at 8:43 Comment(0)
P
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

Pyorrhea answered 22/4, 2019 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.