27

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
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Robinson
  • 349
  • 1
  • 5
  • 11
  • will `os.system('python')` suffice? Or do you need to interact with the variables and objects within the script? – yuvi Oct 06 '13 at 09:03

3 Answers3

37

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

ling
  • 479
  • 1
  • 4
  • 3
  • "thank you" * 1000! I was just looking at http://stackoverflow.com/questions/19329601/interactive-shell-debugging-with-pycharm 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! – tom stratton Jan 16 '15 at 19:58
  • 1
    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. – Li-aung Yip Feb 21 '15 at 12:41
  • 4
    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. – hannes.koller Nov 04 '15 at 10:06
  • This also does not work if your script has any raw_inputs for some reason (it just acts like Enter was immediately pressed) – Justin May 13 '16 at 20:49
  • 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. – Adriaan Sep 04 '17 at 08:19
  • Strangely, this option does not appear for me. I see the option appearing in someone else's [screenshot](https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000749030-How-to-stop-interactive-console-running), but not for me using PyCharm 2019.2.3. – Garrett Oct 02 '19 at 23:33
30

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

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
  • 1
    "Show command line afterwards" seems to be renamed to "Run with Python console", see [this](https://stackoverflow.com/a/50508341/3653185) answer – pj.dewitte Mar 12 '19 at 18:05
9

Click Run -> Edit Configurations...,

Then check the box Run with Python console.

Xiangyi Yan
  • 121
  • 1
  • 4