银河麒麟桌面安装pyautogui后导入失败:display = Display(os.environ['DISPLAY'])

大家好我是Tarzan,今天在银河麒麟安装pyautogui后提示DISPLAY获取不到,uos上目前没有遇到这个问题,记录一下解决办法。

安装pyautogui

  1. 先安装一个库:pip3 install pyscreeze==0.1.28
  2. 安装pyautogui: pip3 install pyautogui==0.9.53

导入报错

>>> import pyautogui                                                                         
Traceback (most recent call last):                                                           
  File "", line 1, in                                                         
  File "/home/kylin/.local/lib/python3.8/site-packages/pyautogui/__init__.py", line 246, in <
module>                                                                                      
    import mouseinfo                                                                         
  File "/home/kylin/.local/lib/python3.8/site-packages/mouseinfo/__init__.py", line 223, in <
module>                                                                                      
    _display = Display(os.environ['DISPLAY'])                                                
  File "/usr/lib/python3.8/os.py", line 675, in __getitem__                                  
    raise KeyError(key) from None                                                            
KeyError: 'DISPLAY'                                                                          
>>>  

解决

  • 解决方法一:打开终端输入:export DISPLAY=:0

  • 解决方法二:

    import os
    os.environ['DISPLAY'] = ':0'
    import pyautogui
    
  • 关于DISPLAY的说法看着:DISPLAY=":0"

一波未平一波又起

刚刚能导入了,结果直接给我退出了:

kylin@kylin-pc:~$ python3
>>> import pyautogui
NOTE: You must install tkinter on Linux to use MouseInfo. Run the following: sudo apt-get install python3-tk python3-dev
kylin@kylin-pc:~$

安装sudo apt-get install python3-tk就可以,因为python3-dev报错

kylin@kylin-pc:~$ sudo apt-get install  python3-dev                                                                         
下列软件包有未满足的依赖关系:                                                                                                                                                                                 
 python3-dev : 依赖: libpython3-dev (= 3.8.2-0kylin2) 但是它将不会被安装                                                                                                                                       
               依赖: python3.8-dev (>= 3.8.2-1~) 但是它将不会被安装                                                                                                                                            
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。

一波又起

截图失败

>>> pyautogui.screenshot('tre.png')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/kylin/.local/lib/python3.8/site-packages/pyscreeze/__init__.py", line 499, in _screenshot_linux
    raise NotImplementedError('"scrot" must be installed to use screenshot functions in Linux. Run: sudo apt-get install scrot')
NotImplementedError: "scrot" must be installed to use screenshot functions in Linux. Run: sudo apt-get install scrot

解决:sudo apt-get install scrot

kylin@kylin-pc:~$ python3
Python 3.8.10 (default, Jan  3 2023, 02:21:00) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyautogui
>>> pyautogui.screenshot('asdf.png')
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=1920x1080 at 0x7F97985E20>
>>> exit()

终于成功的截图了

综上:

pyautogui依赖:

  • sudo apt-get install python3-tk
  • sudo apt-get install scrot
  • pip3 install pyscreeze==0.1.28
  • 当然还有很多依赖可以通过:pipdeptree -p pyautogui查看
posted @ 2023-08-30 15:35  Tarzen  阅读(729)  评论(0编辑  收藏  举报