How can I make one Python file run another?

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, 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 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 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.

Using os.system()

The os.system() function from Python's built-in os module 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 manually.

Syntax

os.system(command)

Where command is a string representing the command to execute, such as "python another_script.py".

Example

First, let's create a helper script ?

# Create helper.py content
helper_content = '''
def main():
    print("Welcome to TutorialsPoint and Have a Happy Learning")

if __name__ == "__main__":
    main()
'''

# Write to file
with open('helper.py', 'w') as f:
    f.write(helper_content)

print("helper.py created successfully!")
helper.py created successfully!

Now we can run this script using os.system() ?

import os

# Run the helper script
os.system("python helper.py")
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, consider subprocess.run().

Using subprocess.run()

The subprocess.run() function from Python's built-in subprocess module provides much more control over how commands are executed and how their output and errors are handled compared to os.system().

Syntax

subprocess.run(args, *, capture_output=False, text=False, shell=False)

Where:

  • args: A list or string of the command to execute, usually as ["python", "script.py"]
  • capture_output: If True, captures stdout and stderr
  • text: If True, treats output as text instead of bytes
  • shell: If True, runs the command through the system shell

Example

Here's how to use subprocess.run() to execute a Python file with error handling ?

import subprocess

def execute_python_file(file_path):
    try:
        result = subprocess.run(['python', file_path], 
                              capture_output=True, text=True)
        
        if result.returncode == 0:
            print("Execution successful.")
            print("Output:")
            print(result.stdout)
        else:
            print(f"Error: Failed to execute '{file_path}'.")
            print("Error output:")
            print(result.stderr)
    except FileNotFoundError:
        print(f"Error: The file '{file_path}' does not exist.")

# Execute the helper script
execute_python_file('helper.py')
Execution successful.
Output:
Welcome to TutorialsPoint and Have a Happy Learning

Using exec()

The exec() function executes dynamically created Python code. Unlike subprocess.run() or os.system(), which run external scripts as separate processes, exec() executes the code in the current Python process.

Syntax

exec(object[, globals[, locals]])

Where:

  • object: A string or compiled code object to be executed
  • globals (Optional): Dictionary specifying the global namespace
  • locals (Optional): Dictionary specifying the local namespace

Example

Here's how to run a Python file using exec() by reading its contents ?

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}")

# Execute the helper script
execute_python_file_with_exec('helper.py')
Welcome to TutorialsPoint and Have a Happy Learning

Using importlib.import_module()

The importlib.import_module() function from Python's built-in importlib module allows dynamic importing of a module by its name at runtime. This method is useful when the module name is known only at runtime.

Syntax

importlib.import_module(name, package=None)

Where:

  • name: The name of the module as a string like 'helper'
  • package (Optional): Used to specify the package name for submodules

Example

Here's how 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(os.path.abspath(module_path))
        module_name = os.path.splitext(os.path.basename(module_path))[0]
        
        if module_dir not in sys.path:
            sys.path.insert(0, module_dir)

        # Import module dynamically
        imported_module = importlib.import_module(module_name)
        print(f"Module '{module_name}' imported successfully!")
        
    except ModuleNotFoundError:
        print(f"Error: The module '{module_name}' could not be found.")
    except Exception as e:
        print(f"An error occurred: {e}")

# Execute the helper script
run_module_from_path('helper.py')
Welcome to TutorialsPoint and Have a Happy Learning
Module 'helper' imported successfully!

Comparison

Method Execution Context Output Capture Best For
os.system() Separate process No Simple script execution
subprocess.run() Separate process Yes Advanced control & error handling
exec() Same process N/A Code sharing & dynamic execution
importlib Same process N/A Module reuse & dynamic imports

Conclusion

Use subprocess.run() for robust script execution with error handling. Use exec() for dynamic code execution in the same process. Use importlib.import_module() for reusable module imports.

Updated on: 2026-03-24T18:15:57+05:30

61K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements