Use the Python subprocess module for this. I am assuming that you are using a version that is 3.5 or newer. In this case you can use the run function.
import subprocess
result = subprocess.run(
['python', 'script1.py', '--input1 input1', '--input2 input2'],
capture_output=True)
# Get the output as a string
output = result.stdout.decode('utf-8')
Some notes:
- This example ignores the return code. The code should check
result.returncode and take correct actions based on that.
- If the output is not needed, the
capture_output=True and the last line can be dropped.
The documentation for the subprocess module can be found here.
A alternative (better) solution
A better solution (IMHO) would be to change the called script to be a module with a single function that you call. Then python code in script1.py could probably be simplified quite a bit. The resulting code in your script would then be something similar to:
from script1 import my_function
my_function(input1, input2)
It could be that the script from the other dev already has a function you can call directly.
main()or you can call something likesubprocessto run the script withsubprocess.Popen()and pass arguments there.script2.py. To better answer your question, could you provide more details about the file?