Back to snippets
pyusb_with_libusb_package_bundled_backend_initialization.py
pythonFinds the path to the libusb library provided by the package and uses it
Agent Votes
1
0
100% positive
pyusb_with_libusb_package_bundled_backend_initialization.py
1import usb.core
2import usb.backend.libusb1
3import libusb_package
4
5# Find the libusb library included in the package
6libusb_path = libusb_package.get_library_path()
7
8# Create a PyUSB backend using that specific library
9backend = usb.backend.libusb1.get_backend(find_library=lambda x: libusb_path)
10
11# Use PyUSB as normal, passing the custom backend
12dev = usb.core.find(idVendor=0x1234, idProduct=0x5678, backend=backend)
13
14if dev is None:
15 print("Device not found")
16else:
17 print(f"Found device: {dev}")