4

I am trying to connect with Python to my USB devices.

The final result should be a connection to my Blood Pressure Monitor but I am failing already to connect to ANY device.

My simple code - which I found here - is bellow. The Product- and Vendor ID I got from Apple Menu > About this Mac > System Information

import usb.core
import usb.util

# find our device
dev = usb.core.find(idVendor=0x0781, idProduct=0x55a4)

# was it found?
if dev is None:
    raise ValueError('Device not found')

# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()

# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]

ep = usb.util.find_descriptor(
    intf,
    # match the first OUT endpoint
    custom_match = \
    lambda e: \
        usb.util.endpoint_direction(e.bEndpointAddress) == \
        usb.util.ENDPOINT_OUT)

assert ep is not None

# write the data
ep.write('test')

But I get always NoBackendError: No backend available from dev = usb.core.find(idVendor=0x0781, idProduct=0x55a4)

For the connection I installed pyusb in my Python env and with Homebrew libusb on my mac.

I have no clue how to get a connection or even a simple list via iteration with all my connected Product- and Vendor IDs.

1 Answers1

9

This error is to be expected if pyusb cannot find the dynamic libraries of libusb.

Installing libusb with Homebrew is not sufficient. Homebrew puts the relevant files in /opt/homebrew/Cellar/libusb/1.0.24/lib and creates symbolic links in /opt/homebrew/lib. But pyusb is not aware of these paths.

You have two main options:

  • Add /opt/homebrew/lib to the environment variable DYLD_LIBRARY_PATH. For a permanent setup, add it to ~/.zshenv:
export DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH"
  • Create a symbolic link in your home directory. This takes advantage of the fact that ~/lib is a default fallback path for libraries:
ln -s /opt/homebrew/lib ~/lib
Codo
  • 75,595
  • 17
  • 168
  • 206
  • I didn't have the `DYLD_LIBRARY_PATH` environment variable, but the second option worked fine. Thanks! – Akenolt May 09 '23 at 10:33