pythonpycharmapplication-restartpython-os

How to restart a program in Pycharm?


For the following code, if it is run in command prompt, the result is: 1 2 3 4 5 6 1 2 3 4 5 6 . . .

If it is run in Pycharm, the result is only: 1 2 3 4 5 6. That is, restart_program() doesn't produce anything in Pycharm.

import sys
import os
def restart_program():
    """Restarts the current program.
    Note: this function does not return. Any cleanup action (like
    saving data) must be done before calling this function."""
    python = sys.executable
    os.execl(python, python, *sys.argv)
if __name__ == "__main__":
    for i in range(1,10,1):
        print i
        if i>5:
            restart_program()

Solution

  • Changing my run configuration to run the script with Python Console did the trick for me.

    Open the "Run" menu (or click the arrow to the left of the run button) and click "Edit Configurations...". Your default configuration should be displayed. In the Configuration->Execution section tick "Run with Python Console" and save the changes.

    Your script will now be executed with the Python Console when using the edited run configuration and restarting should work.