我的代码现在只有 400 多行,我不希望得到答案,而是得到一些可能出错的建议。在我的程序中,我启动了一个子进程:
#out_select is a variable that defaults to the AD9850 control program
#and is switched to the sinetable program with the valve_select command.
subprocess.call(out_select, shell=True)
#The following lines are used to find the PID of the program used for output
#and save it for the stop command to kill that PID.
sp = subprocess.Popen(['python',out_select])
end_sine = int(sp.pid)-1
print end_sine
#end = str(end_sine)
这是通过 tkinter 按钮命令启动的。该程序确实在后台启动,但我 .当我启动命令(通过单击按钮)时,我在 LXTerminal 中收到以下错误消息:
python: can't open file 'python DACscrap.py &': [Errno 2] No such file or directory
或者根据命令out_select:
python: can't open file 'sudo python AD9850_GUI.py &': [Errno 2] No such file or directory
经示波器验证,这两个程序都运行良好,我可以返回程序并使用其他按钮小部件。挂断是程序中有一个图表,我无法通过 USB 端口从 arduino 发送值。这不是 arduino,因为该图将绘制一个零值。关于问题可能出在哪里的任何建议?如果不是这里的几行代码,那么我可以在其他地方做点什么吗?
最佳答案
错误消息意味着您尝试使用 sp = subprocess.Popen(['python',out_select])
启动的 python
可执行文件找不到out_select
变量中包含的文件名(实际上,您不太可能将 Python 脚本存储在名为:'python DACscrap.py &'
的文件中)。
尝试熟悉运行外部进程:
- 不要使用
shell=True
来运行单个命令。而是传递一个列表 - 不要使用
&
(Popen不等待命令退出)
例如,代替 call("cmd 'arg 1' arg2 &", shell=True)
,使用:
p = Popen(['cmd', 'arg 1', 'arg2'])
# ... do something else
p.wait()
导入 Python 模块并调用相应的函数可能更方便,而不是使用 subprocess
将 Python 文件作为脚本运行。参见 Call python script with input with in a python script using subprocess
关于python: 无法打开文件 'python DACscrap.py &' : [Errno 2] 没有这样的文件或目录...但是它有吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35798153/