Registry / http-networking / python-xlib

python-xlib

JSON →
library0.33pypypi✓ verified 26d ago

Python X Library (python-xlib) provides a pure Python interface to the X Window System protocol, allowing Python applications to interact with X servers. As of version 0.33, it supports Python 3.6+ and actively receives bug fixes and extension updates, typically with several releases per year addressing compatibility and adding new X protocol extensions.

pip install python-xlib
INSTALL
IMPORT
SIG · PYTHON-XLIB
P
python-xlib
http-networkingpythonv0.33
Install
1.7s avg
Import
55ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.058s · 19.3MB
glibc
py 3.103.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()
X
import Xlib.X
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.
fix
Update 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()`.
fix
Ensure `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`.
fix
Verify 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.
fix
Install 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.
fix
Check 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.
fix
Ensure 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.
Agent activity
30 hits · last 30 days
node
26
OpenAI (training)
1
Resources
python-xlib — pip install python-xlib · libregistry