Interacting with program after execution
Asked Answered
H

3

28

In PyCharm, after I run a script it automatically kills it:

C:\Users\Sean.virtualenvs\Stanley\Scripts\python.exe C:/Users/Sean/PycharmProjects/Stanley/Stanley.py

Process finished with exit code 0

How can I interact with the script after it starts? For lack of a better way to phrase it, how can I get the

>>>

prompt after the script runs once through?

  • PyCharm Community Edition 3.0
  • Windows 7
  • Python 2.7
Heterocercal answered 6/10, 2013 at 8:44 Comment(1)
will os.system('python') suffice? Or do you need to interact with the variables and objects within the script?Homeric
N
31

UPDATE
Starting with version 4.0 there's an option Show command line afterwards (renamed in later versions to Run with Python console) when editing run/debug configuration in Run|Edit Configurations....


From output of python --help:

-i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x

To set interpreter option in PyCharm go to Run|Edit Configuration

Nicolenicolea answered 6/10, 2013 at 20:1 Comment(1)
"Show command line afterwards" seems to be renamed to "Run with Python console", see this answerAndean
N
38

in Pycharm, Run/Debug menu choose Edit Configuration, check the box before 'Show command line afterwards'

Niedersachsen answered 16/1, 2015 at 6:43 Comment(6)
"thank you" * 1000! I was just looking at #19330101 and this is much, much easier than any of the answers there (though, if you are reading this before that be sure to check it out as the ability to interrupt via the debugger is great too!Vulva
Note: This feature is new in PyCharm 4.0, and received a bugfix in 4.0.1 or 4.0.2 to fix an issue with PYTHONPATH.Felicita
This is ALMOST perfect. It works only as long as the script does not throw an exception. Exceptions close the console. But this can be fixed by adapting the indirection script which Pycharm uses for execution: it is located in $PYCHARM_INSTALL_DIR/helpers/pydev/pydev_run_in_console.py Find the line that says "pydev_imports.execfile(file, globals, locals) # execute the script" and wrap it in a try...except block. Done.Flotilla
This also does not work if your script has any raw_inputs for some reason (it just acts like Enter was immediately pressed)Obstacle
With this solution, it is not possible to use the tab button to complete variable names. Is there any way to also add this functionality. With python -i script.py the tab can be used to complete variable names.Lama
Strangely, this option does not appear for me. I see the option appearing in someone else's screenshot, but not for me using PyCharm 2019.2.3.Ironclad
N
31

UPDATE
Starting with version 4.0 there's an option Show command line afterwards (renamed in later versions to Run with Python console) when editing run/debug configuration in Run|Edit Configurations....


From output of python --help:

-i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x

To set interpreter option in PyCharm go to Run|Edit Configuration

Nicolenicolea answered 6/10, 2013 at 20:1 Comment(1)
"Show command line afterwards" seems to be renamed to "Run with Python console", see this answerAndean
G
10

Click Run -> Edit Configurations...,

Then check the box Run with Python console.

Giblet answered 24/5, 2018 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.