Install & Compatibility
Where this runs
tested against v0.33 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.058s · 19.3MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.052s · 20MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Display
✓ import Xlib.display
display = Xlib.display.Display()
protocol
✓ import Xlib.protocol
This quickstart demonstrates how to connect to an X server, retrieve the root window, and print its ID. It also includes an optional step to create a basic window, which might require a running graphical environment.
import Xlib.display
import Xlib.X
try:
# Connect to the default display
# The DISPLAY environment variable must be set, e.g., export DISPLAY=:0
display = Xlib.display.Display()
# Get the root window of the default screen
root = display.screen().root
print(f"Successfully connected to display: {display.get_display_name()}")
print(f"Root window ID: {root.id}")
# Example: Create a simple black window
# If this causes issues in headless environments, comment out or wrap in try/except
try:
window = root.create_window(
0, 0, 200, 100, 1, # x, y, width, height, border_width
Xlib.X.CopyFromParent, # depth
Xlib.X.InputOutput, # class
Xlib.X.None, # visual
Xlib.X.CWEventMask | Xlib.X.CWBackPixel, # value_mask
event_mask=Xlib.X.ExposureMask, # event_mask
background_pixel=display.screen().black_pixel # background_pixel
)
window.map_request()
window.map()
display.flush()
print("Created a simple window (will be visible briefly or not at all in some environments).")
# In a real app, you'd enter an event loop here.
# For quickstart, we just create and then let it disappear.
except Xlib.error.BadMatch:
print("Could not create window (e.g., no graphical environment or visual not supported).")
finally:
if 'display' in locals() and display:
display.close()
print("Display connection closed.")
Debug
Known issues
breakingThe method `add_extension_error` was renamed to `extension_add_error` in version 0.32.fixUpdate any calls from `obj.add_extension_error()` to `obj.extension_add_error()`.
affects: 0.32+
gotchaPython 3.9+ compatibility issues may arise with `python-xlib` versions older than 0.29 due to the removal of `array.array.tostring()`.fixEnsure `python-xlib` is at version 0.29 or higher when using Python 3.9 or newer. The latest version (0.33) is fully compatible.
affects: <0.29 running on Python 3.9+
gotchaWhen connecting to an X display, the `DISPLAY` environment variable must be correctly set, e.g., `:0` or `localhost:0`. Incorrect or missing settings will lead to `Xlib.error.DisplayNameError`.fixVerify the `DISPLAY` environment variable is correctly configured for your X server. For local sessions, `export DISPLAY=:0` or `export DISPLAY=':0.0'` is common. For remote, ensure SSH X forwarding is enabled or set it to the remote host (e.g., `export DISPLAY=remotehost:0`).
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'Xlib'
The `python-xlib` library is not installed in your current Python environment.
fixInstall the library using pip: `pip install python-xlib`
Xlib.error.DisplayNameError: Can't open display: :0
The X server cannot be reached, often because the `DISPLAY` environment variable is incorrect or the X server is not running/accessible.
fixCheck if an X server is running and accessible. Verify the `DISPLAY` environment variable (e.g., `echo $DISPLAY`) is set correctly. For SSH, ensure X forwarding is enabled (`ssh -X user@host`).
TypeError: an integer is required (got type bytes)
This error often indicates a Python 3 compatibility issue in older `python-xlib` versions, especially when handling event data or protocol messages where byte strings were expected but integers or vice-versa were provided.
fixEnsure you are using `python-xlib` version 0.27 or higher, as several Python 3 compatibility fixes (including event sub-code handling) were introduced in versions 0.24 and 0.27. Upgrade `pip install --upgrade python-xlib`.
Upgrade
Version history
0.33latest on PyPI · released Dec 25, 2022
Audit
Dependencies
PythonrequiredRequires Python 3.6 or newer.