PyUSB

1.3.1 · active · verified Thu Apr 09

PyUSB provides easy USB access for Python, abstracting away the low-level details of USB communication. It allows Python applications to interact with USB devices on various operating systems. The current version is 1.3.1, and it receives updates to fix bugs and maintain compatibility, though releases are not on a fixed schedule.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to find an attached USB device, retrieve its manufacturer and product strings, and properly dispose of resources. Replace `dev = usb.core.find()` with specific `idVendor` and `idProduct` for a targeted search.

import usb.core
import usb.util

# Find the first USB device available. 
# For a real application, you'd specify idVendor and idProduct, e.g.,
# dev = usb.core.find(idVendor=0x046d, idProduct=0xc077) # Example: Logitech mouse

dev = usb.core.find() # Finds any device

if dev is None:
    print("No USB device found. Ensure a device is connected and permissions are set.")
else:
    print(f"Device found: Bus {dev.bus}, Address {dev.address}")
    try:
        # Try to get manufacturer and product strings
        # These indices (iManufacturer, iProduct) might be 0 or invalid on some devices
        manufacturer = usb.util.get_string(dev, dev.iManufacturer)
        product = usb.util.get_string(dev, dev.iProduct)
        print(f"  Manufacturer: {manufacturer}")
        print(f"  Product: {product}")
    except Exception as e:
        print(f"  Could not read device strings (permissions or device issue): {e}")

    # Always remember to dispose resources when done with the device
    usb.util.dispose_resources(dev)
    print("Resources disposed.")

view raw JSON →