HOWTO · Python

How to Restart Script in Python

Learn three ways to restart or rerun a Python script: replace its process with os.execv(), start a child with subprocess.call(), or repeat application logic in a loop.

On this page

To restart a Python script, choose whether you need a new interpreter process or only need to repeat your program’s work. os.execv() replaces the current process, subprocess.call() starts a child process and waits for it, and a loop can run your application logic again without restarting Python.

Method 1: Replace the current process with os.execv()

Use os.execv() when the current interpreter should be replaced by a fresh run of the same script. Pass the active interpreter as the executable and the script’s argument list as its arguments:

os.execv(sys.executable, [sys.executable, *sys.argv])

Import os and sys, then call this expression when the restart condition is met. sys.argv includes the script path and the arguments supplied to it, so the new interpreter receives them again. A successful execv() does not return to the old Python code; the operating system runs the replacement program in the same process slot. Flush any output you need to preserve before replacing the process. This operation is platform-dependent and is not demonstrated as an executed example here.

Method 2: Start a new process with subprocess.call()

Use subprocess.call() when a separate interpreter should run the script while the current process remains its parent. The call is subprocess.call([sys.executable, *sys.argv]); import subprocess and sys first. It waits until the child process finishes, then returns the child’s exit status to the parent. The parent does not disappear automatically, so return or exit from its restart branch after the call if it should stop. The child may inherit the same restart logic, so make sure its condition eventually lets the run finish. This process-launch behavior is not shown as executed output here.

Method 3: Repeat program logic with a loop

If only the application’s work needs to run again, put that work in a function and call it from a controlled loop. This keeps the same interpreter and avoids building a chain of child processes. The example uses a fixed sequence of choices to show two runs and a clean exit; replace the sequence with your input or application condition.

Run this example:

def run_task(number):
    print(f"Run {number}")

for number, action in enumerate(("restart", "restart", "exit"), start=1):
    if action == "exit":
        print("Done")
        break
    run_task(number)
Run 1
Run 2
Done

In a real command-line program, read an action on each pass and call run_task() only when the user asks to repeat it. Give the loop a clear exit condition. Repeating a function does not reset module globals, open resources, or interpreter state; close or reinitialize anything the next run must not reuse.

Choose the right restart method

Use os.execv() to replace a one-off script process, subprocess.call() when the parent must wait for a separate run, and a loop when you want to repeat selected application work. These approaches have different process lifecycles: replacement leaves no old Python frame to resume, a child run keeps the parent alive while it waits, and a loop retains the current interpreter state. For a web service, use its process manager or deployment controls instead of restarting a worker from a request handler; an in-request restart can interrupt users and lose in-memory state.

Frequently asked questions

Which method should I choose?

Choose a loop for repeated application actions, os.execv() to replace the current process, or subprocess.call() when a parent process needs to wait for a new interpreter run.

Can a script restart automatically?

Yes. A condition such as a configuration change can select a restart without prompting a user. Ensure the condition can become false or otherwise end the restart cycle.

Does restarting affect performance?

Starting an interpreter has startup cost, and frequent restarts may repeat initialization work. A loop avoids that process startup but keeps the current interpreter state, which may need explicit cleanup.

Can I pass changed arguments on restart?

Yes. Build a new argument list for os.execv() or subprocess.call() instead of reusing sys.argv unchanged. Keep the interpreter executable as the first item and include the script path before its application arguments.

Should a web application restart its script from a request?

Generally, no. A worker restart during a request can interrupt service and discard in-memory state. Use the server or process manager’s supported restart and deployment mechanism.