Run Python script from another script & pass arguments

In Python programming we often have multiple scripts with multiple functions. What if, if some important and repeated function is in another script and we need to import and run to our main script. So, here in this article we will learn about several methods using which you can Run a Python script from another Python script and pass arguments.

Table Of Contents

Method 1 : Importing in other script

First method we will be using is very simple and easiest one. In this method we can just import the function and then call the function by just passing the arguments. Must keep in mind that both the files should reside in the same folder.

See the example code below

CODE :

code_1.py

# *args has been used to pass variable number of positional arguments.
def expenseCalculator(*expense):
    # sum adds the numbers provided as parameters.
    print('Previous month expense was ',sum(expense))

script.py

# importing the function from another module.
from code_1 import expenseCalculator

# calling the function and by passing arguments.
expenseCalculator(2000,3000)

OUTPUT :

Previous month expense was 5000

So, In the above code and output you can see we have two files code_1.py and script.py. In code_1.py we have a function which takes positional arguments and returns the sum by using the sum() method. This function has been imported in the script.py. When this function is called in the script.py by providing some arguments, the function executes and returns the sum of the arguments provided.

Method 2 : Using os.system and sys.argv

Next method we can use to Run a Python script from another Python script and pass arguments is a combination of two methods first is system() of os module and another is argv() from sys module. Let’s look both of these methods one by one.

os.system : os module comes bundled with python programming language and is used to interact with system or give system commands to our Operating System. system() method is used to execute system commands in a subshell.

sys.argv : This module in python programming language is widely used to interact with the system it provides various function and modules that can communicate with the Python Interpreter and also used to manipulate python runtime environment. sys.argv reads the arguments which are passed to the Python at the time of execution of the code. Arguments provided to the sys.argv is stored in list, first index which is index number 0 is reserved for the file name and then arguments are stored.

Now see the example code below and then we will discuss more about this method.

CODE :

code_1.py

# importing sys module
import sys

# *args has been used to pass variable number of positional arguments. 
def expenseCalculator(*expense):
    # sum adds the numbers provided as parameters.
    print('Previous month expense was ',sum(expense))

# storing the arguments by slicing out the 0 index which holds the file name.
arguments = sys.argv[1:]

# initialized an empty list.
expenses = []

# looping through the arguments to convert it into int.
for i in arguments:
    expenses.append(int(i))

# calling the function and passing the expenses.
expenseCalculator(*expenses)

script.py

# importing the os module.
import os

# running the file code_1.py and passing some arguments.
os.system("python3 code_1.py 2000 3000 5000")

OUTPUT :

Previous month expense was 10000

So, In the code and output above you can see we have two files code_1.py which has function expenseCalculator() which takes the variable number of positional arguments and returns the sum of all the expenses using the sum() function. Also we have sys.argv() file which reads all the arguments which are provided at the time of execution and then using int() function we have converted the arguments which are in string to integer.

Then there is script.py from where we are running another script which is code_1.py. In this file, we have used os.system() to run the code_1.py and also provided some arguments.

So, you can see we have used sys.argv() to read all the arguments which are passed at the time of execution and used os.system() to execute the program with some arguments.

Method 3 : Using subprocess module

Another method we can use to Run a Python script from another Python script and pass arguments is a combination of Popen() and argv() methods.

The Popen() method is of subprocess module which comes pre-installed with Python programming language and is mostly used to execute system commands. It has some features which provide edge over the os module like, this connects to input/output/error pipes and obtains their return codes. This also allows spawning new process. The Popen() method is used to execute system commands via string arguments/commands.

Now See the Example codes below

CODE :

code_1.py

# importing sys module
import sys

# *args has been used to pass variable number of positional arguments. 
def expenseCalculator(*expense):
    # sum adds the numbers provided as parameters.
    print('Previous month expense was ',sum(expense))

# storing the arguments by slicing out the 0 index which holds the file name.
arguments = sys.argv[1:]

# initialized an empty list.
expenses = []

# looping throught the arguments to convert it into int.
for i in arguments:
    expenses.append(int(i))

# calling the function and passing the expenses.
expenseCalculator(*expenses)

script.py

# importing subprocess module
import subprocess

# using Popen() method to execute with some arguments.
subprocess.Popen("python3 code_1.py 2000 3000 4000",shell=True)

OUTPUT :

Previous month expense was 9000

So, In the code and output above you can see we have used Popen() method to run a python script and argv() to read arguments. The code_1.py file is same as the method 2 but in script.py file instead of system() we have used Popen() to pass arguments. The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.

SUMMARY

So In this Python tutorial article we learned about several methods using which we can Run a Python script from another Python script and pass arguments. We learned about sys.argv() using which which we can read the arguments passed at the time of execution in the command prompt. Also we learned about two other methods Popen() of subprocess module and system() of os module using which we can execute the system commands or here we have used to run the Python script from another Python script and also passed some arguments at the time of execution.

You can always use any of the methods above, but most easy to understand and implement is the first method. Make sure to read and write example codes in order to have a better understanding of this problem.

Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top