Registry / devops / pyudev

pyudev

JSON →
library0.24.4pypypi✓ verified 25d ago

pyudev is an LGPL-licensed, pure Python binding for libudev, the device and hardware management library for Linux. It provides functionality to enumerate devices, query their properties and attributes, and monitor device events, including asynchronous monitoring with various GUI toolkit integrations. The current version is 0.24.4 and it supports CPython 3.9 and newer. It is actively maintained with development on GitHub.

pip install pyudev
INSTALL
IMPORT
SIG · PYUDEV
P
pyudev
devopspythonv0.24.4
Install
1.5s avg
Import
51ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.24.4 · 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.048s · 18.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.054s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Context
from pyudev import Context
Device
from pyudev import Device
Monitor
from pyudev import Monitor
MonitorObserver
from pyudev import MonitorObserver
Devices
from pyudev import Devices

This quickstart initializes a udev context, then demonstrates enumerating all block device partitions and printing their file system labels. It also shows how to set up a monitor to listen for new USB device 'add' events in real-time.

import pyudev # Create a udev context context = pyudev.Context() # Enumerate all block devices that are partitions and print their labels print('Listing partitions:') for device in context.list_devices(subsystem='block', DEVTYPE='partition'): # Use device.get() for properties, providing a default for safety label = device.get('ID_FS_LABEL', 'unlabeled partition') print(f" {device.device_node}: {label}") # Monitor for new USB devices (example) print('\nMonitoring for new USB devices (Ctrl+C to stop):') monitor = pyudev.Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') monitor.start() # Start the monitor to receive events for action, device in monitor: if action == 'add': print(f" Added USB device: {device.sys_name} ({device.get('ID_VENDOR_FROM_DATABASE', 'Unknown Vendor')})")
Debug
Known issues
gotchaUsing a `Device` object in a boolean context (e.g., `if device:`) can yield `False` if the device has no udev properties, even if the device itself is valid. This is due to `Device` inheriting from `Mapping`. Explicitly check for `None` or specific properties instead.
fix
Avoid `if device:`. Instead, use `if device is not None:` or `if 'PROPERTY_NAME' in device:` if checking for specific data.
affects: >=0.21.0
deprecatedAccessing udev properties directly via `Device` object indexing (e.g., `device['PROPERTY']`) is deprecated since version 0.21.0. The `Device` class no longer behaves like a mapping for properties.
fix
Use the `Device.get('PROPERTY_NAME', default_value)` method or `device.properties.get('PROPERTY_NAME', default_value)` instead.
affects: >=0.21.0
gotchaDo not use object identity (`is` operator) to compare `Device` objects (e.g., `device1 is device2`). `pyudev` may create multiple `Device` objects that refer to the same underlying udev device. Compare them by value using `==` or `!=` instead.
fix
Use `if device1 == device2:` for comparison.
affects: All
deprecatedMethods like `Device.from_sys_path()`, `Device.from_name()`, and `Device.from_device_file()` were deprecated in version 0.18.
fix
Use the static methods on the `Devices` class, such as `pyudev.Devices.from_sys_path(context, path)`, `pyudev.Devices.from_name(context, subsystem, name)`, or `pyudev.Devices.from_device_file(context, filename)`.
affects: >=0.18
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyudev'
The `pyudev` library has not been installed in your current Python environment.
fix
pip install pyudev
ImportError: libudev.so.1: cannot open shared object file: No such file or directory
The underlying `libudev` shared library, which `pyudev` depends on, is not installed or cannot be found on your system.
fix
Install the `libudev` development package for your Linux distribution (e.g., `sudo apt-get install libudev-dev` on Debian/Ubuntu, `sudo yum install libudev-devel` on RHEL/Fedora).
PermissionError: [Errno 13] Permission denied
Your current user lacks the necessary read/write permissions to access udev interfaces or specific device files.
fix
Run the script with `sudo`, or add your user to the `plugdev` or `dialout` groups (e.g., `sudo usermod -a -G plugdev $USER` then log out and back in).
AttributeError: 'Device' object has no attribute 'vendor_id'
`pyudev.Device` objects do not have direct attributes like `vendor_id`; these properties are stored in the `properties` dictionary.
fix
Access device properties using the `device.properties` dictionary with the correct uppercase key, e.g., `device.properties.get('ID_VENDOR_ID')`.
Upgrade
Version history
0.24.4latest on PyPI · released Oct 8, 2025
Audit
Dependencies
libudevrequiredCore system library that pyudev binds to. Must be installed on the host system (e.g., via apt, dnf, or pacman).
PyQt4/PyQt5/PySide/pygobject/wxPythonoptionalOptional dependencies for integrating device monitoring into respective GUI toolkit event loops.
Agent activity
5 hits · last 30 days
node
4
Resources
pyudev — pip install pyudev · libregistry