我正在尝试使用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;
答案 0 :(得分:0)
尝试xhost +
并通过DISPLAY
之类的方法在python中手动导出os.environ['DISPLAY']=:1
变量,其中:1是DISPLAY变量的内容。