要从终端启动 python 脚本(运行 OLED 显示器需要它),我必须使用以下 bash 命令:python demo_oled_v01.py --display ssd1351 --width 128 --height 128 --interface spi --gpio-data-command 20
..py
之后的那些参数很重要,否则,脚本将以默认设置运行,在我的情况下,脚本将不会以默认设置启动.因此,需要这些参数.
To launch a python script (it is needed for running an OLED display) from terminal, I have to use the following bash commands: python demo_oled_v01.py --display ssd1351 --width 128 --height 128 --interface spi --gpio-data-command 20
. Those parameters after .py
are important, otherwise, the script will run with default settings and in my case, the script will not launch with default settings. Thus, those parameters are needed.
当我需要从另一个 python 脚本启动我的脚本(而不是在终端上使用 bash 命令)时,就会出现问题.从父脚本启动我的 python 脚本之一.我用过:
The problem arises when I need to launch my script from another python script, (instead of using bash commands on terminal). To launch one of my python script from a parent script. I have used:
import subprocess # to use subprocess
p = subprocess.Popen(['python', 'demo_oled_v01.py --display ssd1351 --width 128 --height 128 --interface spi --gpio-data-command 20'])
在我的父脚本中,但我收到一条错误消息:
in my parent script but I got an error stating:
python: 无法打开文件 'demo_oled_v01.py --display ssd1351 --width 128 --height 128 --interface spi --gpio-data-command 20': [Errno 2] 没有这样的文件或目录
我怀疑在 .py
之后添加参数 --display ssd1351 --width 128 --height 128 --interface spi --gpio-data-command 20
可能会导致启动脚本困难.如前所述,这些参数对于我在终端上使用 bash 命令启动是必不可少的.如何使用带有所需参数的子进程来启动此脚本?
I suspect that adding the parameters --display ssd1351 --width 128 --height 128 --interface spi --gpio-data-command 20
after .py
may be causing difficulty in launching the script. As mentioned, these parameters are otherwise essential for me to include for launching with bash commands on terminal. How can I use subprocess with the required parameters to launch this script?
subprocess
库正在解释您的所有参数,包括 demo_oled_v01.py
作为单个参数Python.这就是为什么 python 抱怨它无法找到具有该名称的文件的原因.尝试运行它:
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'])
查看更多关于 Popen 的信息这里.
See more information on Popen here.
这篇关于从另一个脚本启动 python 脚本,参数在子进程参数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!