Run Another Python Script with Arguments in Python for optimal execution.

By muntashah
complete Guide on How to Run Another Python script

Running a Python script from another script while passing arguments allows developers to modularise the program. The OS module or any other subprocess executes the external script, and arguments are appended to the command line.

The blog teaches developers how to run another Python script with arguments in Python. We have discussed three approaches. Continue reading to check out!

How do I run another Python script with arguments in Python?

There are three approaches to run a Python script with arguments in Pythons, and they are:

  1. Using the subprocess module
  2. Using exec
  3. Using the importlib module

Let’s talk about each one of them,

Using Subprocess Module

You can run a Python script from another script using the subprocess module. Here, you can use the subprocess.run function to execute the called_script.py script from caller_script.py and pass arg1, arg2, and arg3 (arguments) as command-line arguments while calling the script. You can later retrieve these arguments using sys.argv and print them.

Example Code:

caller_script.py:
import subprocess
# Pass arguments to the script
arg1 = "tutorial"
arg2 = "for"
arg3 = "developers"
# You should now run the script along with the arguments
subprocess.run(['python', 'called_script.py', arg1, arg2, arg3])

called_script.py:
import sys
# You can now retrieve arguments passed from the calling script
arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]
# code to display the received arguments
print(f"Arguments received: {arg1}, {arg2}, {arg3}")

Now, Run the Caller Script:

python caller_script.py

Output: Arguments received: tutorial, for, developers

Using exec

You can also run a Python script from another script using exec. The exec function in Python executes dynamically created Python code or scripts. 

caller_script.py:
# First define arguments
arg1 = "tutorial"
arg2 = "for"
arg3 = "developers"
# Second, execute called script along with the arguments
exec(open("called_script.py").read(), {
'arg1': arg1, 'arg2': arg2, 'arg3': arg3})

called_script.py:
# Next, retrieve arguments
arg1 = globals().get('arg1', None)
arg2 = globals().get('arg2', None)
arg3 = globals().get('arg3', None)
# Finally, print arguments
print(f"Arguments received: {arg1}, {arg2}, {arg3}")

Now, Run the Caller Script

 python caller_script.py

Output: Arguments received: tutorial, for, developers

Using importlib module

You can also run a Python script from another script using the importlib module. This module dynamically imports modules and can be used to execute Python scripts. You can import the script as a module and then call its functions to pass arguments to them.
 

Example Code

caller_script.py:
import importlib.util
# Start with defining the arguments
arg1 = "tutorial"
arg2 = "for"
arg3 = "developers"
# Second import called script as module
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)
#Next call function with the arguments
called_script.main(arg1, arg2, arg3)

called_script.py:
def main(arg1, arg2, arg3):
# Print arguments
print(f"Arguments received: {arg1}, {arg2}, {arg3}")
if __name__ == "__main__":
import sys
# Retrieve arguments
arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]
# Call the main function
main(arg1, arg2, arg3)
 

Now, Run the Caller Script:

python caller_script.py

Output: Arguments received: tutorial, for, developers

Conclusion

These three methods allow developers to run another Python script with arguments in Python, where "arg1," "arg2," etc., can be your choice. You can choose any of the three methods that best fit your requirements. 

share on :

Profile picture for user muntashah
muntashah
I am an avid writer who enjoys the world of computer science. My strength lies in delivering tech points in easy-to-understand words.