Run a Python script from another Python script, passing in arguments
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Running one Python script from another is common in automation, orchestration, and CLI wrappers. The right solution depends on whether you truly need a separate process. If you do, subprocess with an explicit argument list is the safe default because it gives you correct quoting, exit codes, output capture, and timeout control.
Decide Whether You Need a New Process
Before spawning another script, ask whether importing a function would be better.
Use an import when:
- you control both files
- you want direct return values
- you do not need process isolation
Use a child process when:
- the second script is an independent command-line tool
- it should run with its own process state
- you need a separate exit code or timeout boundary
If the child logic is really just reusable code, refactoring it into a module is usually cleaner than making one script shell out to another.
Use subprocess.run with an Argument List
The safe pattern is to pass a list of arguments rather than building one shell command string.
sys.executable is important because it uses the current Python interpreter, which is especially useful in virtual environments. Passing a list instead of a shell string avoids quoting bugs and reduces injection risk.
Parse the Child Arguments with argparse
The parent process is only half of the design. The child script should define a clear interface too.
This makes the boundary between parent and child explicit. Once both scripts treat the interaction as a small CLI contract, debugging gets much easier.
Handle Failures and Timeouts
Production code should not assume the child succeeds or finishes quickly.
check=True tells Python to raise an exception on a non-zero exit code. That is often better than manually checking returncode everywhere.
Pass Structured Data Carefully
If the child needs many parameters, command-line flags can become awkward. For moderate payloads, JSON can work.
Parent:
Child:
For large or sensitive data, a file, pipe, or environment variable may be better than long command-line arguments that can appear in process listings.
Control the Working Directory and Environment
Do not let the child script depend implicitly on whatever shell state happened to launch the parent.
Setting cwd and env explicitly makes the run more reproducible and easier to diagnose.
When You Need Streaming Output
subprocess.run waits for the child to finish. If you want to stream output while it runs, use Popen.
That is useful for long-running tasks where progress output matters.
Common Pitfalls
The most common mistake is building a shell command string manually instead of passing an argument list. Another is using "python" literally instead of sys.executable, which breaks in virtual environments or systems with multiple Python installations. Developers also ignore exit codes, leaving failed child scripts looking successful just because they printed partial output. Finally, passing secrets on the command line can expose them in process listings or logs.
Summary
- Use
subprocess.runwith an argument list for safe child-script execution. - Use
sys.executableso the child runs with the expected Python interpreter. - Define the child interface clearly with
argparse. - Handle non-zero exits and timeouts explicitly.
- Set working directory and environment deliberately when reproducibility matters.
Related reading
- Run celery task without workers
- Run Class methods in threads python
- Run inference using Onnx model in python?
- Run localhost server in Google Colab notebook
- Run manage.py from AWS EB Linux instance
- Run py.test test in different process
- Run Tensorflow unit tests
- Running a single test from unittest.TestCase via the command line
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.