Naydachi Kamikadze
Naydachi Kamikadze

Reputation: 107

How run python script with arguments via Python?

I have simple command and it's perfect work from cmd. But me need that I can run this using subprocess.call or subprocess.Popen function.

cmd command:

python3  <some directory>/tools/upload.py --chip esp8266 --port COM8 --baud 115200 --before default_reset --after hard_reset write_flash 0x0 file.bin 

My try

subprocess.call(["python",
                 "<some directory>/tools/upload.py",
                 "--chip esp8266",
                 "--port COM8",
                 "--baud 115200",
                 "--before default_reset",
                 "--after hard_reset",
                 "write_flash 0x0",
                 "file.bin"])

For example this provide Arduino Console:

python3 C:\Users\User\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.7.4/tools/upload.py --chip esp8266 --port COM8 --baud 115200 --before default_reset --after hard_reset write_flash 0x0 C:\Users\User\AppData\Local\Temp\arduino_build_424950/Esp8266-lwmqtt.ino.bin 

And I can run this command from cmd and Makefile

Upvotes: 0

Views: 140

Answers (2)

Konrad Rudolph
Konrad Rudolph

Reputation: 546073

One problem with your subprocess command is that you are passing the arguments incorrectly. Those parts that are space-separated on the command line need to go into separate items in the argument list:

subprocess.call(["python",
                 "<some directory>/tools/upload.py",
                 "--chip", "esp8266",
                 "--port", "COM8",
                 "--baud", "115200",
                 "--before", "default_reset",
                 "--after", "hard_reset",
                 "write_flash", "0x0",
                 "file.bin"])

If that still doesn’t work you need to show us the exact error message you’re getting.

Upvotes: 1

Andrei R
Andrei R

Reputation: 26

Shouldn't it be run using python3 ? e.g.:

subprocess.call(["python3",
                 "<some directory>/tools/upload.py",
                 "--chip esp8266",
                 "--port COM8",
                 "--baud 115200",
                 "--before default_reset",
                 "--after hard_reset",
                 "write_flash 0x0",
                 "-file.bin"])

Make sure python3 is added to the Path, but as long as it work from cmd, you should be ok. Similar to https://stackoverflow.com/a/22258715/5498515.

Upvotes: 1

Related Questions