How to Run Another Python script with Arguments in Python
Last Updated :
26 Feb, 2024
Running a Python script from another script and passing arguments allows you to modularize code and enhance reusability. This process involves using a subprocess or os module to execute the external script, and passing arguments can be achieved by appending them to the command line. In this article, we will explore different approaches to Running a Python Script From Another Script and Pass Arguments.
Run Python Script from Another Script and Pass Arguments
Below are the approaches to run a Python script from another Script and Pass Arguments
- Using the subprocess module
- Using exec
- Using the importlib module
Run Python Script From Another Script Using subprocess module
In this example, the subprocess.run function is used to execute the called_script.py script from caller_script.py. The arguments (arg1, arg2, arg3) are passed as command-line arguments when calling the script. The called script then retrieves these arguments using sys.argv and prints them.
caller_script.py
Python3
import subprocess
arg1 = "Geeks"
arg2 = "for"
arg3 = "Geeks"
subprocess.run([ 'python' , 'called_script.py' , arg1, arg2, arg3])
|
called_script.py
Python3
import sys
arg1 = sys.argv[ 1 ]
arg2 = sys.argv[ 2 ]
arg3 = sys.argv[ 3 ]
print (f "Arguments received: {arg1}, {arg2}, {arg3}" )
|
Run the Caller Script:
python caller_script.py
Output:
Arguments received: Geeks, for, Geeks
Run Python Script From Another Script Using exec
In this example, we use the exec function in Python that executes dynamically created Python code or scripts. We can use this method to execute a Python script within another script and pass arguments to it.
caller_script.py
Python3
arg1 = "Geeks"
arg2 = "for"
arg3 = "Geeks"
exec ( open ( "called_script.py" ).read(), {
'arg1' : arg1, 'arg2' : arg2, 'arg3' : arg3})
|
called_script.py
Python3
arg1 = globals ().get( 'arg1' , None )
arg2 = globals ().get( 'arg2' , None )
arg3 = globals ().get( 'arg3' , None )
print (f "Arguments received: {arg1}, {arg2}, {arg3}" )
|
Run the Caller Script
python caller_script.py
Output:
Arguments received: Geeks, for, Geeks
Run Python Script From Another Script Using importlib module
In this example, we use the importlib module for the dynamically importing modules. While it’s typically used for importing modules, we can also use it to execute Python scripts. By importing the script as a module we can call its functions and pass arguments to them.
caller_script.py
Python3
import importlib.util
arg1 = "Geeks"
arg2 = "for"
arg3 = "Geeks"
spec = importlib.util.spec_from_file_location(
"called_script" , "called_script.py" )
called_script = importlib.util.module_from_spec(spec)
spec.loader.exec_module(called_script)
called_script.main(arg1, arg2, arg3)
|
called_script.py
Python3
def main(arg1, arg2, arg3):
print (f "Arguments received: {arg1}, {arg2}, {arg3}" )
if __name__ = = "__main__" :
import sys
arg1 = sys.argv[ 1 ]
arg2 = sys.argv[ 2 ]
arg3 = sys.argv[ 3 ]
main(arg1, arg2, arg3)
|
Run the Caller Script:
python caller_script.py
Output:
Arguments received: Geeks, for, Geeks
Share your thoughts in the comments
Please Login to comment...