Problems importing python-Xlib

2020-06-08 06:43发布

I installed a new module and it appears as if one of its dependencies was not already installed. The module is called Xlib.display. Here is the error message I received:

from Xlib.display import Display
ImportError: No module named Xlib.display

Where can I find this module that I am apparently lacking? Google yielded no leads.

"Edit: I already have that sourceforge module downloaded but I still get the same resutls.

8条回答
我想做一个坏孩纸
2楼-- · 2020-06-08 07:03

I was having the same problem, but the solutions above didn't work for me. Since I had installed python through the anaconda package, when I used:

sudo apt-get install  python-xlib

Xlib was still undetectable by python2. The solution in my case was to use:

anaconda search -t conda python-xlib

Then find the package from the anaconda api, mine was erik/python-xlib. Install it using:

conda install --channel https://conda.anaconda.org/erik python-xlib

Then it worked.

查看更多
贼婆χ
3楼-- · 2020-06-08 07:05

Scenario:

I was trying to use screenshot functionalities of pyautogui package. I was getting this error:

Traceback (most recent call last):
  File "test_screenshot.py", line 1, in <module>
    import pyautogui
  File ".../miniconda3/envs/myenv/lib/python3.7/site-packages/pyautogui/__init__.py", line 152, in <module>
    from . import _pyautogui_x11 as platformModule
  File ".../miniconda3/envs/myenv/lib/python3.7/site-packages/pyautogui/_pyautogui_x11.py", line 7, in <module>
    from Xlib.display import Display
ModuleNotFoundError: No module named 'Xlib'

Python code (test_screenshot.py):

import pyautogui
img = pyautogui.screenshot('test.png')

Environment:

  • Ubuntu 16.04 (LTS)
  • conda 4.5.11
  • Python 3.7 (Miniconda)
  • requirements.txt:

    certifi==2019.3.9
    Pillow==5.4.1
    PyAutoGUI==0.9.42
    PyGetWindow==0.0.4
    PyMsgBox==1.0.6
    PyRect==0.1.4
    PyScreeze==0.1.20
    PyTweening==1.0.3
    

Solution:

I installed python-xlib package in the conda environment using:

pip install python-xlib

Now test_screenshot.py is running without any error.

Updated requirements.txt:

certifi==2019.3.9
Pillow==5.4.1
PyAutoGUI==0.9.42
PyGetWindow==0.0.4
PyMsgBox==1.0.6
PyRect==0.1.4
PyScreeze==0.1.20
python-xlib==0.25
PyTweening==1.0.3
six==1.12.0
查看更多
Summer. ? 凉城
4楼-- · 2020-06-08 07:07

I honestly cant explain why this works... but here is the command that got it working for me.

sudo apt-get install python3-xlib

Should not work because xlib apparently does not work with python 3.x, but everything installed alright, so I'm not complaining!

查看更多
来,给爷笑一个
5楼-- · 2020-06-08 07:12

Below worked for me!

pip install python3_xlib

I have also used pyuserinput for automation which requires this.

查看更多
别忘想泡老子
6楼-- · 2020-06-08 07:15

Please try.

This shall install Xlib sudo apt-get install python-xlib

Then you can check

>>from Xlib.display import Display

To install PyMouse if you want to control and capture mouse events please use:

sudo easy_install https://github.com/pepijndevos/PyMouse/zipball/master

查看更多
再贱就再见
7楼-- · 2020-06-08 07:17

I don't think the Xlib library works in Python 3.

Source:

Requirements

The Python X Library requires Python 1.5.2 or newer. It has been tested to various extents with Python 1.5.2 and 2.0 through 2.6.

查看更多
登录 后发表回答