Interacting with program after execution
Solution 1
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
Solution 2
in Pycharm, Run/Debug menu choose Edit Configuration, check the box before 'Show command line afterwards'
Solution 3
Click Run -> Edit Configurations...
,
Then check the box Run with Python console
.
Related videos on Youtube
Robinson
Updated on June 18, 2022Comments
-
Robinson about 2 years
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
-
yuvi over 10 yearswill
os.system('python')
suffice? Or do you need to interact with the variables and objects within the script?
-
tom stratton over 9 years"thank you" * 1000! I was just looking at stackoverflow.com/questions/19329601/… 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!
-
Li-aung Yip over 9 yearsNote: 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.
-
hannes.koller over 8 yearsThis 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.
-
Justin about 8 yearsThis also does not work if your script has any raw_inputs for some reason (it just acts like Enter was immediately pressed)
-
Adriaan almost 7 yearsWith 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. -
pj.dewitte over 5 years"Show command line afterwards" seems to be renamed to "Run with Python console", see this answer
-
Garrett almost 5 yearsStrangely, 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.