简体   繁体   English

如何通过 Python 运行带参数的 Python 脚本?

[英]How run python script with arguments via Python?

I have simple command and it's perfect work from cmd.我有简单的命令,它是 cmd 的完美工作。 But me need that I can run this using subprocess.call or subprocess.Popen function.但我需要我可以使用 subprocess.call 或 subprocess.Popen 函数运行它。

cmd command : cmd命令

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:例如,这提供了 Arduino 控制台:

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我可以从 cmd 和 Makefile 运行这个命令

Shouldn't it be run using python3 ?它不应该使用 python3 运行吗? eg:例如:

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.确保将 python3 添加到路径中,但只要它从 cmd 工作,你应该没问题。 Similar to https://stackoverflow.com/a/22258715/5498515 .类似于https://stackoverflow.com/a/22258715/5498515

One problem with your subprocess command is that you are passing the arguments incorrectly.您的subprocess命令的一个问题是您传递的参数不正确。 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.如果这仍然不起作用,您需要向我们展示您收到的确切错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM