0

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

2 Answers2

1

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.

Andrei R
  • 26
  • 2
  • I try repeat recommendation from this ticket, but it's not work for me. My python environment add to Path variable, it's work for Makefile, it's work for cmd, but not work from Python – Naydachi Kamikadze Aug 28 '20 at 08:43
  • If your call works from cmd, might as well try: subprocess.run(["python3", "/tools/upload.py", "--chip esp8266", "--port COM8", "--baud 115200", "--before default_reset", "--after hard_reset", "write_flash 0x0", "-file.bin"], shell=True ) Please note the set of shell=True. – Andrei R Aug 28 '20 at 12:39
1

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Traceback (most recent call last): File "C:/Users/User/Documents/ArduinoData/packages/esp8266/hardware/esp8266/2.7.4/tools/upload.py", line 54, in cmdline = cmdline + [write_addr, binary] NameError: name 'binary' is not defined – Naydachi Kamikadze Aug 28 '20 at 08:54
  • 1
    @NaydachiKamikadze This tells us that your other application has a bug that’s completely unrelated to your calling code: it’s trying to use an undefined variable `binary`. You need to fix your `upload.py`. – Konrad Rudolph Aug 28 '20 at 08:56
  • I didn't immediately notice a change in your version. It's work, for whitespace need new argument case. – Naydachi Kamikadze Aug 28 '20 at 13:39