从另一个脚本启动python脚本,并在subprocess参数中添加参数 [英] Launch a python script from another script, with parameters in subprocess argument

查看:233
本文介绍了从另一个脚本启动python脚本,并在subprocess参数中添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要从终端启动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: can't open file 'demo_oled_v01.py --display ssd1351 --width 128 --height 128 --interface spi --gpio-data-command 20': [Errno 2] No such file or directory

我怀疑在.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脚本,并在subprocess参数中添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆