Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Bash和$DISPLAY,如何?_Python_Bash_Environment - Fatal编程技术网

Python Bash和$DISPLAY,如何?

Python Bash和$DISPLAY,如何?,python,bash,environment,Python,Bash,Environment,我正在开发一个pythongui应用程序,在某个时候,我需要推迟大部分Python代码的执行。我曾尝试使用at来执行此操作: line = 'echo "python ./executor.py ibm ide graph" | at -t 1403211632' subprocess.Popen(line,Shell=True) 这一行没有错误,在给定的时间有效地开始作业 现在,executor.py的每个选项都是它必须执行的作业,每个作业都有一个try/catch日志保护。在某些情况下,我

我正在开发一个pythongui应用程序,在某个时候,我需要推迟大部分Python代码的执行。我曾尝试使用
at
来执行此操作:

line = 'echo "python ./executor.py ibm ide graph" | at -t 1403211632'
subprocess.Popen(line,Shell=True)
这一行没有错误,在给定的时间有效地开始作业

现在,
executor.py
的每个选项都是它必须执行的作业,每个作业都有一个try/catch日志保护。在某些情况下,我会发现以下错误:

14-03-21_17:07:00 starting ibm for Simulations/140321170659
Failed to execute ibm : no display name and no $DISPLAY environment variable
Aborted the whole execution.
我尝试了以下方法,认为我可以向环境提供$DISPLAY,但没有成功(相同的错误):

从<代码>处的人:

The working  directory, the environment (except for the variables BASH_VERSINFO, DISPLAY, EUID, GROUPS, SHELLOPTS, TERM, UID, and _) and the umask are retained from the time of invocation.
问题:
  • 引发此错误的原因可能是什么
  • 如何在的环境中向
    提供$DISPLAY变量
解决方案: 实际上,我需要将
export DISPLAY=:0.0
放入echo中,以便在
at
启动其环境后进行设置

line = echo "export DISPLAY=:0.0; python..." | at...
subprocess.Popen(line,Shell=True)

您需要在python脚本中设置
DISPLAY
,方法是获取当前环境,添加DISPLAY设置,并将新环境传递给Popen创建的子shell

import os;
new_env = dict(os.environ)
new_env['DISPLAY'] = '0.0'
...
...
subprocess.Popen(line, env=new_env, Shell=True)

尝试
export DISPLAY=0.0
尝试使用
export DISPLAY=0.0
export DISPLAY=:0.0
。同样的错误。我的错误思维很糟糕<代码>导出显示=:0.0;echo“python…”at…
不起作用<代码>echo“导出显示=:0.0;python…”at…可以工作。谢谢。我确实得到了完全相同的错误。我认为,
at
只是在他自己的位置上运行,与我调用命令的Shell分离。您可以关闭Shell,但作业仍处于挂起状态,
at
将以任何方式启动它,而不显示$DISPLAY。我们得到了解决方案,我们必须在
at
启动其环境后设置变量。编辑了我的帖子以显示解决方案。
import os;
new_env = dict(os.environ)
new_env['DISPLAY'] = '0.0'
...
...
subprocess.Popen(line, env=new_env, Shell=True)