Problems importing python-Xlib

2020-06-08 06:25发布

问题:

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.

回答1:

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



回答2:

Below worked for me!

pip install python3_xlib

I have also used pyuserinput for automation which requires this.



回答3:

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.



回答4:

On Debian systems install python-xlib.

On other systems there's a high probability that the package carries the same name.



回答5:

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.



回答6:

I was looking for the same answer, however after some more digging it seems that XCB (X protocol C-language Binding) will obsolete Xlib in general. From the XCB website:

The X protocol C-language Binding (XCB) is a replacement for Xlib featuring a small footprint, latency hiding, direct access to the protocol, improved threading support, and extensibility.

Fortunately there are python bindings available as python-xpyb in apt or xpyb on PyPi. I've not gotten that far in my project so I haven't tested if this works with Python3, but this is probably the way to go and the proper place to file any Python3 support bugs if necessary.



回答7:

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!



回答8:

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