Home > OS >  Python seems unable to find hid library on Mac
Python seems unable to find hid library on Mac

Time:11-16

When I set up a python virtual environment and install the hid module it doesn't seem able to find a library that I've installed with MacPorts. I'm running Mac OS Big Sur 11.6.1 and I've also installed python with MacPorts. Any troubleshooting ideas?

comp:~ me$ virtualenv-3.9 vtest
created virtual environment CPython3.9.8.final.0-64 in 475ms
  creator CPython3Posix(dest=/Users/me/vtest, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/me/Library/Application Support/virtualenv)
    added seed packages: pip==21.3.1, setuptools==58.3.0, wheel==0.37.0
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
comp:~ me$ source vtest/bin/activate
(vtest) comp:~ me$ python3 -m pip install hid
Collecting hid
  Using cached hid-1.0.4-py3-none-any.whl
Installing collected packages: hid
Successfully installed hid-1.0.4
(vtest) comp:~ me$ python3 -c "import hid"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/me/vtest/lib/python3.9/site-packages/hid/__init__.py", line 30, in <module>
    raise ImportError(error)
ImportError: Unable to load any of the following libraries:libhidapi-hidraw.so libhidapi-hidraw.so.0 libhidapi-libusb.so libhidapi-libusb.so.0 libhidapi-iohidmanager.so libhidapi-iohidmanager.so.0 libhidapi.dylib hidapi.dll libhidapi-0.dll
(vtest) comp:~ me$ for i in libhidapi-hidraw.so libhidapi-hidraw.so.0 libhidapi-libusb.so libhidapi-libusb.so.0 libhidapi-iohidmanager.so libhidapi-iohidmanager.so.0 libhidapi.dylib hidapi.dll libhidapi-0.dll;do find /opt -name "$i";done
/opt/local/lib/libhidapi.dylib

Update: hidapi is installed with MacPorts:

(vtest) comp:~ me$ sudo port installed | grep hid
  hidapi @0.9.0_0 (active)

CodePudding user response:

Your interpreter in your virtual environment is supposed to be segregated from the dependencies on your machine (to a point), so in that sense, it is working as designed.

Check out Set LD_LIBRARY_PATH before importing in python. This should get you going.

CodePudding user response:

So your problem in fact isn't that your python can't find the hid package.

When importing hid, hid tries to load native libraries that are not installed. this can be solved (on MacOS) using brew install hidapi as described here

  • Related