Run a Python script from another Python script, passing in arguments
Try using os.system
:
os.system("script2.py 1")
execfile
is different because it is designed to run a sequence of Python statements in the current execution context. That's why sys.argv
didn't change for you.
Launch a python script from another script, with parameters in subprocess argument
The subprocess
library is interpreting all of your arguments, including demo_oled_v01.py
as a single argument to python. That's why python is complaining that it cannot locate a file with that name. Try running it as:
p = subprocess.Popen(['python', 'demo_oled_v01.py', '--display',
'ssd1351', '--width', '128', '--height', '128', '--interface', 'spi',
'--gpio-data-command', '20'])
See more information on Popen here.
run python script with arguments in another python script
Use subprocess.run()
: https://docs.python.org/3.8/library/subprocess.html#subprocess.run
import subprocess
subprocess.run(["csdl-to-json.py", "--input", "<CSDL-Dir>", "--output", "<JSON-Dir>"])
Edited, using the following:
subprocess.call(["python3", "/home/sundeep/Desktop/csdl-to-json/script.py", "--input", "/home/sundeep/Desktop/csdl-to-json/metadata", "--output", "/home/sundeep/Desktop/csdl-to-json/json"])
How to call a python file that uses flags from another python script?
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.
How to execute a python script file with an argument from inside another python script file
The best answer is don't. Write your getCameras.py as
import stuff1
import stuff2
import sys
def main(arg1, arg2):
# do whatever and return 0 for success and an
# integer x, 1 <= x <= 256 for failure
if __name__=='__main__':
sys.exit(main(sys.argv[1], sys.argv[2]))
From your other script, you can then do
import getCamera
getCamera.main(arg1, arg2)
or call any other functions in getCamera.py
Running one python script from another script with command line argument having executable in it
Imagine I have 2 files, the first file is a.py
and the other is b.py
and I want to call the a.py
from b.py
.
The content of a.py is:
print('this is the a.py file')
and the content of b.py is:
import os
stream = os.popen('python3 a.py')
output = stream.read()
print(output)
Now when I call b.py
from terminal I get the output I expect which is a.py print statment
user@mos ~ % python3 b.py
this is the a.py file
You can do this with subprocess
too instead of os
module.
Here is a nice blog I found online where I got the code from: https://janakiev.com/blog/python-shell-commands/
Run a python script from another python script and pass variables to it
You can make use of the subprocess module do perform external commands. It is best to start with much simpler commands first to get the gist of it all. Below is a dummy example:
import subprocess
from subprocess import PIPE
def main():
process = subprocess.Popen('echo %USERNAME%', stdout=PIPE, shell=True)
username = process.communicate()[0]
print username #prints the username of the account you're logged in as
process = subprocess.call('python py1.py --help', shell=True)
process = subprocess.call('python py2.py --help', shell=True)
process = subprocess.call('python py3.py --help', shell=True)
if __name__ == '__main__':
main()
This will grab the output from echo %USERNAME%
and store it. It will also run your three scripts but do nothing fancy with them. You can PIPE
the output of the scripts as shown in the first example and feed them back in to your next script.
This is NOT the only way to do this (you can import your other scripts). This is nice if you would like to have an external master script to control and manipulate all your child scripts.
If you haven't checked our argparse yet, you should.
Related Topics
How to Delete a Character in an Item in a List (Python)
Permissionerror: [Errno 13] Permission Denied Flask.Run()
Cast String to Float Is Not Supported in Linear Model
How to Print Colored Text to the Terminal
Make Alternate Letters Capital
How to Limit Iterations of a Loop in Python
How to Get Rid of the B-Prefix in a String in Python
Running Two Python Scripts With Bash File
Collecting and Reporting Pytest Results
Write a Program That Find the Largest Integer in a String
Get Character Position in Alphabet
Interactive Matplotlib Figures in Google Colab
Adding Months to a Pandas Object in Python
How to Download Multiple Files or an Entire Folder from Google Colab
Return the First Key in Dictionary - Python 3