{"id":4226,"library":"pyudev","title":"pyudev: libudev Binding","description":"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.","status":"active","version":"0.24.4","language":"en","source_language":"en","source_url":"https://github.com/pyudev/pyudev","tags":["linux","udev","hardware","device management","system"],"install":[{"cmd":"pip install pyudev","lang":"bash","label":"Install pyudev"}],"dependencies":[{"reason":"Core system library that pyudev binds to. Must be installed on the host system (e.g., via apt, dnf, or pacman).","package":"libudev","optional":false},{"reason":"Optional dependencies for integrating device monitoring into respective GUI toolkit event loops.","package":"PyQt4/PyQt5/PySide/pygobject/wxPython","optional":true}],"imports":[{"symbol":"Context","correct":"from pyudev import Context"},{"symbol":"Device","correct":"from pyudev import Device"},{"symbol":"Monitor","correct":"from pyudev import Monitor"},{"symbol":"MonitorObserver","correct":"from pyudev import MonitorObserver"},{"symbol":"Devices","correct":"from pyudev import Devices"}],"quickstart":{"code":"import pyudev\n\n# Create a udev context\ncontext = pyudev.Context()\n\n# Enumerate all block devices that are partitions and print their labels\nprint('Listing partitions:')\nfor device in context.list_devices(subsystem='block', DEVTYPE='partition'):\n    # Use device.get() for properties, providing a default for safety\n    label = device.get('ID_FS_LABEL', 'unlabeled partition')\n    print(f\"  {device.device_node}: {label}\")\n\n# Monitor for new USB devices (example)\nprint('\\nMonitoring for new USB devices (Ctrl+C to stop):')\nmonitor = pyudev.Monitor.from_netlink(context)\nmonitor.filter_by(subsystem='usb')\nmonitor.start() # Start the monitor to receive events\n\nfor action, device in monitor:\n    if action == 'add':\n        print(f\"  Added USB device: {device.sys_name} ({device.get('ID_VENDOR_FROM_DATABASE', 'Unknown Vendor')})\")","lang":"python","description":"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."},"warnings":[{"fix":"Avoid `if device:`. Instead, use `if device is not None:` or `if 'PROPERTY_NAME' in device:` if checking for specific data.","message":"Using 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.","severity":"gotcha","affected_versions":">=0.21.0"},{"fix":"Use the `Device.get('PROPERTY_NAME', default_value)` method or `device.properties.get('PROPERTY_NAME', default_value)` instead.","message":"Accessing 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.","severity":"deprecated","affected_versions":">=0.21.0"},{"fix":"Use `if device1 == device2:` for comparison.","message":"Do 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.","severity":"gotcha","affected_versions":"All"},{"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)`.","message":"Methods like `Device.from_sys_path()`, `Device.from_name()`, and `Device.from_device_file()` were deprecated in version 0.18.","severity":"deprecated","affected_versions":">=0.18"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}