Explain Codes LogoExplain Codes Logo

Run a Python script from another Python script, passing in arguments

python
prompt-engineering
functions
process-control
Anton ShumikhinbyAnton Shumikhin·Oct 12, 2024
TLDR

Here's how you run another script with arguments using the subprocess module:

import subprocess # Get on your marks! subprocess.run(['python', 'other_script.py', 'arg1', 'arg2'])

Replace 'other_script.py' with your script filename, and 'arg1', 'arg2' with your actual arguments.

Choosing the right command-execution tool

Python offers several tools to execute commands and scripts. Among them, subprocess.run() is handy for safe and cross-platform operations. But let's dive deeper into the toolbox:

subprocess.Popen() for more control

Handling asynchronous operations or wanting greater control of the child process? subprocess.Popen() is your friend:

import subprocess proc = subprocess.Popen(['python', 'other_script.py', 'arg1', 'arg2']) # Don't call me Pythonista for nothing! stdout, stderr = proc.communicate()

subprocess.Popen() lets you deal with non-blocking calls and dynamic interaction with spawned process's I/O streams.

Changing sys.argv temporarily

Sometimes, you need to pretend you're on the command line. For this, you can temporary change sys.argv:

import sys import contextlib # Tread carefully! Ninja code ahead! @contextlib.contextmanager def switch_argv(new_args): temp = sys.argv sys.argv = new_args yield sys.argv = temp with switch_argv(['my_script.py', 'arg1', 'arg2']): # Call your function that relies on sys.argv here

This let's you simulate command line arguments without shelling out a new process.

Direct function calls

You remember PYTHONPATH, right? Use it to import functions between scripts for direct interaction:

# My_script.py import other_script # Who needs the command line? Not Python! other_script.main('arg1', 'arg2')

Here, make sure main() function in other_script.py can handle your arguments.

Handling command-line arguments

Implement your argument parsing logic in other_script.py:

import argparse if __name__ == "__main__": parser = argparse.ArgumentParser(description='Process the arguments.') parser.add_argument('arg1', help='Argument 1') # Does 3 arguments mean I am popular now? parser.add_argument('arg2', help='Argument 2') args = parser.parse_args() # Oh yeah, args.arg1 and args.arg2 now all yours.

Your other script now behaves just like a mini command line utility!

Advanced process control with subprocess.Popen()

Getting serious? Use subprocess.Popen() for redirection, deadlock avoidance and running in a shell:

import subprocess proc = subprocess.Popen(['python', 'other_script.py', 'arg1', 'arg2'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) # It's like wiretapping, but for geeks. stdout, stderr = proc.communicate()

Note that spawned processes can be tricky to manage. But with great power comes great responsibility!