当从php调用python脚本时,DISPLAY会出现pyautogui错误

时间:2017-05-11 11:51:55

标签: php python python-3.x pyautogui

我正在尝试使用pyautogui模块。如果我从控制台运行脚本,一切都按预期工作。当我尝试从php(shell_exec)执行python脚本时,我在apache2错误日志中收到以下错误:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    import pyautogui
  File "/usr/local/lib/python3.5/dist-packages/pyautogui/__init__.py", line 115, in <module>
    from . import _pyautogui_x11 as platformModule
  File "/usr/local/lib/python3.5/dist-packages/pyautogui/_pyautogui_x11.py", line 160, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/usr/lib/python3.5/os.py", line 725, in __getitem__
    raise KeyError(key) from None
KeyError: 'DISPLAY'

这是我的python scipt:

#! /usr/bin/python3
import pyautogui
import sys

print("Trying to get info")
pyautogui.click(3500,102,1)

这是php:

$command = escapeshellcmd('/usr/bin/python3 test.py');
$output = shell_exec($command);
echo $output;

1 个答案:

答案 0 :(得分:0)

尝试xhost +并通过DISPLAY之类的方法在python中手动导出os.environ['DISPLAY']=:1变量,其中:1是DISPLAY变量的内容。