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 pyudevVerified import paths — ran on the pinned version, not inferred.
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.
Avoid `if device:`. Instead, use `if device is not None:` or `if 'PROPERTY_NAME' in device:` if checking for specific data.
Use the `Device.get('PROPERTY_NAME', default_value)` method or `device.properties.get('PROPERTY_NAME', default_value)` instead.Use `if device1 == device2:` for comparison.
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)`.
pip install pyudev
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).
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).
Access device properties using the `device.properties` dictionary with the correct uppercase key, e.g., `device.properties.get('ID_VENDOR_ID')`.