
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Run Python Files Together
In Python, it is often necessary to execute another file. This is common in cases such as running helper scripts, triggering workflows, or executing test cases.
Python provides several ways to execute one file using another. Depending on the requirement, whether we want to reuse functions, execute a file directly, or run it as a subprocess. Let us look at these solutions one by one -
Understanding Python Script Execution
Before proceeding with how to run one Python file from another, it's important to understand how Python scripts are executed.
When we run a Python file using python myscript.py in the command prompt, then Python interprets and executes the code line by line from top to bottom.
Each file that we run in this way is treated as a script, and Python automatically sets a special built-in variable called __name__
to "__main__"
In that file.
This allows the developers to write scripts that can either be run directly or imported as modules without executing everything immediately.
# helper.py def greet(): print("Hello from helper!") if __name__ == "__main__": greet() # This will run only if helper.py is executed directly
When the script helper.py
is run directly, it prints the output as Hello from helper!
. But when it's imported into another file, the code if __name__ == "__main__"
does not run. This mechanism gives developers control over how code is executed and reused across files.
The following are the different ways to make one Python file run another file -
Using os.system()
The os.system() is a function from Python's built-in module, which is named os. This allows us to run shell or command prompt commands directly from a Python script. We can use it to execute another Python file as if we were typing the command in the terminal or command prompt manually. Following is the syntax of the os.system() function -
os.system(command)
Where,
-
command: A string representing the command to execute. This could be a system command or a Python script call like
"python another_script.py"
.
Example
In this example, we are using the os.system() function to run another Python script from and within our current script. When we run the below code, it will execute helper.py in a new system shell, just like running it from the command prompt or terminal -
import os os.system("python D:\Tutorialspoint\Articles\helper.py") #Replace with the path to your script
Below is the output of the above code, which uses os.system() function -
Welcome to Tutorialspoint and Have a Happy Learning
Note:
- os.system() is simple but has limited control over the subprocess, like capturing output.
- For more advanced usage, such as reading output and handling errors, we have to consider the subprocess.run().
Using subprocess.run()
Python subprocess.run() is one more function in Python's built-in subprocess module. It is used to run shell commands or execute other Python scripts from within a script. When compared to subprocess.run with os.system(), it gives us much more control over how the command is executed and how its output and errors are handled.
Below is the syntax of using the subprocess.run() -
subprocess.run(args, *, capture_output=False, text=False, shell=False)
Where,
- args: A list or string of the command to execute, which is usually given as a list like ["python", "script.py"].
- capture_output: If this parameter is True, then it captures stdout and stderr.
- text: If this parameter is True, then it treats output as text instead of bytes.
- shell: If this parameter is True, then it runs the command through the system shell.
Example
In this example, we are using the function subprocess.run() method to run one Python file from another file -
import os import subprocess def execute_python_file(file_path): try: completed_process = subprocess.run(['python', file_path], capture_output=True, text=True) if completed_process.returncode == 0: print("Execution successful.") print("Output:") print(completed_process.stdout) else: print(f"Error: Failed to execute '{file_path}'.") print("Error output:") print(completed_process.stderr) except FileNotFoundError: print(f"Error: The file '{file_path}' does not exist.") file_path = r"D:\Tutorialspoint\Articles\helper.py" execute_python_file(file_path)
Following is the output of the above code, which used subprocess.run() method ?
Execution successful. Output: Welcome to Tutorialspoint and Have a Happy Learning
Using exec()
The exec() function is used to execute dynamically created Python code. It executes the Python script passed to it as a string or as a code object. Unlike subprocess.run() or os.system(), which run external scripts as separate processes, the exec() function executes the code in the current Python process itself.
Here is the syntax of using the exec() function -
exec(object[, globals[, locals]])
Where,
- object: A string or compiled code object to be executed.
- globals(Optional): A dictionary to specify the global namespace in which the code is executed.
- locals(Optional): A dictionary to specify the local namespace in which the code is executed.
Example
In this example, we are using the exec() function to run a Python file by reading its contents and executing them within the current script context -
def execute_python_file_with_exec(file_path): try: with open(file_path, 'r') as file: code = file.read() exec(code) except FileNotFoundError: print(f"Error: The file '{file_path}' does not exist.") except Exception as e: print(f"An error occurred: {e}") file_path = r"D:\Tutorialspoint\Articles\helper.py" execute_python_file_with_exec(file_path)
Following is the output of the above example, in here we use exec() function -
Welcome to Tutorialspoint and Have a Happy Learning
Using importlib.import_module()
The importlib.import_module() is a function in Python's built-in importlib module that allows dynamic importing of a module by its name at runtime. This method is useful when the module name is known only at runtime or needs to be imported based on certain conditions.
Below is the syntax of using the importlib.import_module() ?
importlib.import_module(name, package=None)
Where,
- name: The name of the module you want to import as a string like 'helper'.
- package(Optional): It is used to specify the package name when importing a submodule.
Example
In this example, we are using the function importlib.import_module() to dynamically import and run another Python script -
import importlib import sys import os def run_module_from_path(module_path): try: # Add module's directory to sys.path module_dir = os.path.dirname(module_path) module_name = os.path.splitext(os.path.basename(module_path))[0] sys.path.insert(0, module_dir) # Import module dynamically imported_module = importlib.import_module(module_name) # Optionally call a function if needed # imported_module.main() except ModuleNotFoundError: print(f"Error: The module '{module_name}' could not be found.") except Exception as e: print(f"An error occurred: {e}") file_path = r"D:\Tutorialspoint\Articles\helper.py" run_module_from_path(file_path)
Following is the output of the above code, which uses importlib.import_module() method -
Welcome to Tutorialspoint and Have a Happy Learning