How to Run Another Python script with Arguments in Python
Last Updated :
28 Apr, 2025
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
# Arguments to be passed to the called script
arg1 = "Geeks"
arg2 = "for"
arg3 = "Geeks"
# Run the called script with arguments
subprocess.run(['python', 'called_script.py', arg1, arg2, arg3])
called_script.py
Python3
import sys
# Retrieve arguments passed from the calling script
arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]
# Display the received arguments
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
# Define arguments
arg1 = "Geeks"
arg2 = "for"
arg3 = "Geeks"
# Execute called script with the arguments
exec(open("called_script.py").read(), {
'arg1': arg1, 'arg2': arg2, 'arg3': arg3})
called_script.py
Python3
# Retrieve arguments
arg1 = globals().get('arg1', None)
arg2 = globals().get('arg2', None)
arg3 = globals().get('arg3', None)
# Print arguments
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
# Define arguments
arg1 = "Geeks"
arg2 = "for"
arg3 = "Geeks"
# 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)
# Call function with the arguments
called_script.main(arg1, arg2, arg3)
called_script.py
Python3
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 main function
main(arg1, arg2, arg3)
Run the Caller Script:
python caller_script.py
Output:
Arguments received: Geeks, for, Geeks
Similar Reads
Run One Python Script From Another in Python In Python, we can run one file from another using the import statement for integrating functions or modules, exec() function for dynamic code execution, subprocess module for running a script as a separate process, or os.system() function for executing a command to run another Python file within the
5 min read
How Can I Make One Python File Run Another File? In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Ma
2 min read
How to Keep a Python Script Output Window Open? We have the task of how to keep a Python script output window open in Python. This article will show some generally used methods of how to keep a Python script output window open in Python. Keeping a Python script output window open after execution is a common challenge, especially when running scri
2 min read
How To Save Python Scripts In Linux Via The Terminal? Linux users like to do tasks using the terminal as it gives the user more power to do anything with the system whether it is to store, create, or remove files. Other than file management and system control tasks, the terminal is used to write scripts for any programming language. Python and Bash scr
6 min read
How Use Linux Command In Python Using System.Os Using the system module from the os library in Python allows you to interact with the Linux command line directly from your Python script. This module provides a way to execute system commands, enabling you to automate various tasks by integrating Linux commands seamlessly into your Python code. Whe
3 min read
How to Import Other Python Files? We have a task of how to import other Python Files. In this article, we will see how to import other Python Files. Python's modular and reusable nature is one of its strengths, allowing developers to organize their code into separate files and modules. Importing files in Python enables you to reuse
3 min read
How to Call the main() Function of an Imported Module in Python We are given an imported module and our task is to call the main() function of that module after importing it in Python. In this article, we will see how to call the main() of an imported module in Python. Call the main() Function of an Imported Module in PythonBelow, are the code methods of how to
3 min read
Uses of OS and Sys in Python In this article, we will see where we use Os and Sys in Python with the help of code examples. What is Os Module?Python OS module in Python furnishes a versatile means of engaging with the operating system. It facilitates a range of operations, including the creation, deletion, renaming, movement, a
4 min read
Print Output from Os.System in Python In Python, the os.system() function is often used to execute shell commands from within a script. However, capturing and printing the output of these commands can be a bit tricky. This article will guide you through the process of executing a command using os.system() and printing the resulting valu
3 min read
Os Module Vs. Sys Module In Python Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you
5 min read