Python                                                          Home : www.sharetechnote.com

 

 

 

 

Python - Calling a Python script from Another Script

 

You can call (run) another python script from your script. In this way, you can implement various functionalities in multiple different python script file and combine those functionalities.

 

 

Example 01 >

 

 

 

RunScript.py --------------------------------

 

import os

 

print("Running the script : test.py....\n")

os.system('python test.py')

 

 

test.py --------------------------------

 

import os

 

print("Running the script : test.py....\n")

os.system('python test.py')

 

 

You can run the script and get the result as shown below.

 

 

 

 

Example 02 >

 

 

If you have difficulties in understanding RunScript.py due to sys.system(), see Python in Python :Calling Another Script

 

RunScript.py --------------------------------

 

import os

import sys

 

print("Running the script : ", sys.argv[1], "....\n")

os.system('python ' + sys.argv[1])

 

 

test.py --------------------------------

 

import os

 

print("Running the script : test.py....\n")

os.system('python test.py')

 

 

You can run the script and get the result as shown below.