无法在Windows上使用Python库“hid”加载hidapi

ia2d9nvy  于 2023-01-06  发布在  Windows
关注(0)|答案(1)|浏览(1104)

如果问题问得不好,请表示歉意)
我正在尝试使用Python library hid,它依赖于hidapi library。hid似乎无法加载hidapi,因为它在这里告诉我:

$ python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import hid
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[...]\Python310\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

问题是我已经从hidapi的发布页面下载了其中一个库(Windows x64版本),但是我把它放在哪里似乎并不重要,它不工作。

$ echo $PATH
[...]:/c/Program Files/hidapi:[...]

$ ls -lh "/c/Program Files/hidapi"
total 3.0M
-rwxr-xr-x 1 [...] 197121  98K Jan 28 14:38 hidapi.dll*
-rw-r--r-- 1 [...] 197121 5.7K Jan 28 14:38 hidapi.lib
-rw-r--r-- 1 [...] 197121 2.9M Jan 28 14:38 hidapi.pdb

即使我把它放在运行Python的文件夹中,或者放在System32中,同样的错误也会再次发生。

**编辑:**我没有解决核心问题,但是尝试加载库的ctypes库没有跳过它,因为它找不到它,而是因为它不是一个有效的Win32应用程序([WinError 193] %1 n’est pas une application Win32 valide是错误,虽然在法语中)。也许是因为它没有注册,但是我没有成功地尝试注册它。

我找到了一个解决方法:在装入hid库之前手工装入hidapi库

import ctypes
ctypes.CDLL('[my path to the DLL]\\hidapi.dll')
import hid
yh2wf1be

yh2wf1be1#

我把hidapi.dllhidapi.lib放进C:\Users\<Username>\AppData\Local\Programs\Python\Python310,结果成功了!:-)

相关问题