Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

ImportError: failed to locate hidapi shared object on macOS Catalina #17

Closed
Jaharmi opened this issue Jun 12, 2020 · 4 comments
Closed

Comments

@Jaharmi
Copy link

Jaharmi commented Jun 12, 2020

I've gotten an hidapi error on macOS Catalina (v10.15.5, build 19F101). I saw there was an earlier issue and changes back and forth between using hidapi and libusb.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/me/python-virtual-environments/rgb_led_status/lib/python3.7/site-packages/blynclight/__init__.py", line 34, in <module>
    from .blynclight import BlyncLight
  File "/Users/me/python-virtual-environments/rgb_led_status/lib/python3.7/site-packages/blynclight/blynclight.py", line 5, in <module>
    from .hid import HidDevice
  File "/Users/me/python-virtual-environments/rgb_led_status/lib/python3.7/site-packages/blynclight/hid/__init__.py", line 99, in <module>
    raise ImportError("failed to locate hidapi shared object")
ImportError: failed to locate hidapi shared object

My Python3 virtual environment was freshly created with Python 3.7.3 from the Python.org installer. I installed libusb (libusb @1.0.23) and hidapi (hidapi @0.9.0_0) with MacPorts.

$ sudo port selfupdate
$ sudo port upgrade outdated
$ sudo port install libusb 
$ sudo port install hidapi

Here is what I see with system_profiler SPUSBDataType. I have the Blynclight plugged into a port on an old Apple Cinema Display HD, which is plugged into the computer.

Blynclight Standard:
  Product ID: 0x000c
  Vendor ID: 0x2c0d
  Version: 0.01
  Speed: Up to 12 Mb/s
  Location ID: 0x14810000 / 34
  Current Available (mA): 500
  Current Required (mA): 100
  Extra Operating Current (mA): 0

I'm still messing around to see if I can figure things out, but I figured I'd file an issue in case it's known not to work with Catalina or something.

@JnyJny
Copy link
Owner

JnyJny commented Jun 17, 2020

The error in your traceback comes from ctypes.util.find_library failing to find the installed hidapi library.

import ctypes.util

print(ctypes.util.find_library('hidapi'))

I'm not familiar with port so I don't know how it manages the files it installs.

@Jaharmi
Copy link
Author

Jaharmi commented Jun 19, 2020

Since reporting the issue, I was able to get this to work:

  1. Tried to compile hidapi directly on macOS Catalina, which I couldn't get to work.
  2. Compiled hidapi on macOS Mojave, which installed it to /usr/local/lib.
  3. I was able to use hidapi and this module on Mojave to control a Blynclight. Yay!
  4. Copied that and installed the hidapi files to the same location on macOS Catalina.
  5. I was able to use this module on Catalina to control a Blynclight.
  6. Profit!

So, it seems like the problem is either with a) what the MacPorts port command produced or b) with the location where it was installed. MacPorts installs under /opt.

Assuming it compiled correctly, I'm wondering if the /opt location is or can be searched for the library. I'm not sure how to check — or set — that for libraries (like PATH for executables).

From MacPorts:

$ ls -1 /opt/local/lib/ | grep hid
libhidapi.0.dylib
libhidapi.a
libhidapi.dylib

What I copied from macOS Mojave:

$ ls -1 /usr/local/lib/ | grep hid
libhidapi.0.dylib
libhidapi.a
libhidapi.dylib
libhidapi.la

Update: I found these references:

The MacPorts path of /opt is not in the list, per the Stackoverflow article. I'm still not sure how to / if it's possible to add a path for ctypes.util.find_library to search.

Update 2: Also found something that appears similar:

I will keep looking to find a way to add /opt.

@JnyJny
Copy link
Owner

JnyJny commented Jun 19, 2020

Thanks for all the detailed information on what you had to do to get BlyncLight running!

I'll dig into find_library and see if I can improve how the shared library is located.

@JnyJny
Copy link
Owner

JnyJny commented Jun 19, 2020

So looking at the code for ctypes.util.macholib.dylib I found an environment variable you can set to help find_library .. uh.. find the library.

$ export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib

I tested this on my macOS 10.15.5 installation after installing Mac Ports:

$ which port
/opt/local/bin/port
$ brew uninstall hidapi
...
$ blync -a
<blows up>
$ sudo port install hidapi
...
$ blync -a
<still blows up, expected>
$ export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
$ blync -a
Number of available lights: 1
                 KEY:ID:VALUE
                path:00:IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/TRP0@7/IOPP/PXSX@0/IOPP/pci-bridge@2/IOPP/pci1b21,1242@0/AppleUSBXHCIPCI@00000000/AppleUSB20XHCIPort@00300000/Blynclight Plus@00300000/IOUSBHostInterface@0/AppleUserUSBHostHIDDevice
           vendor_id:00:0x2c0d
          product_id:00:0x10
      release_number:00:0x100
      product_string:00:Blynclight Plus
          usage_page:00:0xff00
               usage:00:0x1
    interface_number:00:0x0

Add that to your shell's profile script and you should be able to skip copying things to /usr/local.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants