Search code examples
pythonusbcythonlibusb

'import hid' in Python results in error "hid.so: undefined symbol: libusb_open"


I'm trying to install cython-hidapi to read a USB on my Ubuntu 12.04 (Precise Pangolin). I've followed the instructions from cython-hidapi and installed the following versions:

  • lib-usb == 1.0.9
  • hidapi == 0.7.0
  • cython == 0.16
  • python == 2.7
  • cython-hidapi == latest checkout

When I execute the test part from the installation (python > import hid), I receive the following error:

Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hid
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python2.7/dist-packages/hid.so: undefined symbol: libusb_open

Why do I receive this error and what can I check / do about it?


Solution

  • I've been struggling with this exact same issue over the past week. Luckily one of my friends that knows the Cython world quite well was able to help. You need to change the setup(...) function in setup.py as follows:

    setup(
        cmdclass = {'build_ext': build_ext},
        ext_modules = [Extension("hid", ["hid.pyx", "hid-libusb.c"],
                      libraries=["usb-1.0", "udev", "rt"])]
    )
    

    I don't know whether there will be slight variances on other distributions, but this has been tested on Ubuntu 12.04 and Debian 0.1.12. Use the suggestions in reply to your original post to determine the correct linker flags (LDFLAGS) and for the libraries= line.

    A pull request have been submitted to the maintainer. You can also get the change from my fork.