Python Run Another Python Script

Vaibhhav Khetarpal Jan 05, 2023 May 01, 2021
  1. Use the import Statement to Run a Python Script in Another Python Script
  2. Use the execfile() Method to Run a Python Script in Another Python Script
  3. Use the subprocess Module to Run a Python Script in Another Python Script
Python Run Another Python Script

A basic text file containing Python code that is intended to be directly executed by the client is typically called a script, formally known as a top-level program file.

Scripts are meant to be directly executed in Python. Learning to run scripts and code is a fundamental skill to learn in the world of python programming. Python script usually has the extension '.py'. If the script is run on a windows machine, it might have an extension, .pyw.

This tutorial will discuss different methods to run a Python script inside another Python script.

Use the import Statement to Run a Python Script in Another Python Script

The import statement is used to import several modules to the Python code. It is used to get access to a particular code from a module. This method uses the import statement to import the script in the Python code and uses it as a module. Modules can be defined as a file that contains Python definitions and statements.

The following code uses the import statement to run a Python script in another Python script.

  • Script1.py:
def func1():
    print ("Function 1 is active")

if __name__ == '__main__':
    # Script2.py executed as script
    # do something
    func1()

Output:

Function 1 is active
  • Script2.py:
import Script1.py

def func2():
    print("Function 2 is active") 

if __name__ == '__main__':
    # Script2.py executed as script
    # do something
    func2()
    Script1.func1()

Output:

Function 2 is active
Function 1 is active

Use the execfile() Method to Run a Python Script in Another Python Script

The execfile() function executes the desired file in the interpreter. This function only works in Python 2. In Python 3, the execfile() function was removed, but the same thing can be achieved in Python 3 using the exec() method.

The following code uses the execfile() function to run a Python script in another Python script.

  • Script2.py:
# Python 2 code
execfile("Script1.py")

Output:

Function 1 is active

The same thing can be done in Python 3 by using:

  • Script2.py:
exec(open("Script1.py").read())

Output:

Function 1 is active

Use the subprocess Module to Run a Python Script in Another Python Script

The subprocess module is capable of spawning new processes and can also return their outputs. This is a new module and is intended to replace several older modules like os.system, which was previously used to run a Python script in another Python script.

The following code uses the subprocess module to run a Python script in another Python script.

  • Script1.py:
def func1():
    print ("Function 1 is active")

if __name__ == '__main__':
    # Script2.py executed as script
    # do something
    func1()
  • Script2.py:
import subprocess

subprocess.call("Script1.py", shell=True)

Output:

Function 1 is active

Although all three methods work just fine, this method holds an advantage over the other two methods. Editing an existing Python script and putting all the code it contains into a subroutine is not needed in this method.

Vaibhhav Khetarpal avatar Vaibhhav Khetarpal avatar

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

LinkedIn