Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't load hidapi with Python library "hid" on Windows

sorry if the question is not well asked)

I'm trying to use the Python library hid, which rely on the hidapi library. hid seems to not be able to load hidapi, as it's telling me here:

$ 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

The problem is that I have downloaded one of those libraries from hidapi's release page (the Windows x64 version), but it doesn't seem to matter where I put it, it doesn't work. It is in my PATH:

$ 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

Even if I put it in the folder where I'm running Python from, or in System32, the same error happens again. This post didn't worked either

EDIT: I didn't solve the core problem, but the ctypes library that is trying to load the library isn't skiping it because it can't find it but because it isn't a valid Win32 app ([WinError 193] %1 n’est pas une application Win32 valide is the error, in french though). Maybe it is be cause it's not registered, but I'm not sucessful at trying to register it.

I've found a workaround: manually load the hidapi library before loading the hid library

import ctypes
ctypes.CDLL('[my path to the DLL]\\hidapi.dll')
import hid
like image 662
Neozetare Avatar asked Dec 29 '25 13:12

Neozetare


1 Answers

  1. Uninstall current hid module
pip uninstall hid
  1. Install hidapi and Cython
pip install hidapi Cython
  1. Follow this steps:
git clone https://github.com/trezor/cython-hidapi.git  
cd cython-hidapi  
git submodule update --init    
python setup.py build 
python setup.py install   
pip install -e .
  1. It should be working. Run follow test script
python try.py
like image 73
Leon Kok Seng Avatar answered Jan 01 '26 02:01

Leon Kok Seng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!