Python 似乎无法在 Mac 上找到 hid 库

Python seems unable to find hid library on Mac

当我设置 python 虚拟环境并安装 hid 模块时,它似乎无法找到我使用 MacPorts 安装的库。我是 运行 Mac OS Big Sur 11.6.1,我还安装了 python 和 MacPorts。任何故障排除想法?

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

更新:hidapi 安装有 Mac端口:

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

所以你的问题实际上不是你的 python 找不到 hid 包。

导入 hid 时,hid 会尝试加载未安装的本机库。这可以解决(在 MacOS 上)使用 brew install hidapi 如上所述 here

您的虚拟环境中的解释器应该与您机器上的依赖项隔离(在某种程度上),因此从这个意义上说,它按设计工作。

查看 Set LD_LIBRARY_PATH before importing in python。这应该让你继续。