Calling one python script from another

Ideally should should import requited python file using import into another and call required function from other programs but there could be some instances where you would need to trigger one python script from another.

ThisĀ  is fairly simple.

Lets say I have following two program located in same folder and Let us try calling from is fairly simple

import os

os.system('python3.6 ' + '')

This code will trigger

Passing parameters to calling file

This needs minor changes. Whatever you need to pass, just mention that value or variable after a space.

import os

os.system('python3.6 ' + ' 11 ')

import sys
print("---This is inside script 2")

input_value = int(sys.argv[1])
print(" 0 ", sys.argv[0], )
print(" 1 ", sys.argv[1], type(sys.argv[0]))

Please note, 0th parameter is always the script name, you can pass multiple parameters.

Also, received files are always string, you need to change to required datatype using datatype conversion operators e.g. int(sys.argv[1])

Trigger file located at different directory

import os

file_path = '/home/user/code/quant/source/library/'
os.system('python3.6 ' + file_path + ' 11 22')

Please note the training '/' in file_path


Leave a Reply

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