Search code examples
pythonimportusbpyusb

'import usb.core' doesn't work in Python/PyUSB


I am following the tutorial Programming with PyUSB 1.0.

I am on Windows XP SP3. My Python version is 2.7, and I downloaded and installed the pyusb-1.0.0-a1.zip file.

And libusb-win32-bin-1.2.4.0.zip.

import usb

This works fine but

import usb.core

This doesn't work at all. The error message says

Traceback (most recent call last):
  File "D:\py\usb.py", line 1, in <module>
    from usb import core
  File "D:\py\usb.py", line 1, in <module>
    from usb import core
ImportError: cannot import name core

PS:

"from usb import core"

this makes

Traceback (most recent call last):
  File "D:\py\usb.py", line 1, in <module>
    from usb import core
  File "D:\py\usb.py", line 1, in <module>
    from usb import core
ImportError: cannot import name core

The full source code is here

from usb import core

# Find device
dev = usb.core.find(idVendor=0x1516, idProduct=0x8628)

# Found?
if dev is None:
        raise ValueError('device not found')

# Set the active configuration. Without any arguments, 
# the first configuration will be the active one
dev.set_configuration()

# Get an endpoint instance
ep = usb.util.find_descriptor(
    dev.get_interface_altsetting(), # The first interface

    # Match the first Out Endpoint
    custom_match = \
        lambda e: \
            usb.util.endpoint_direction(e.bEndpointAddress) == \
            usb.util.ENDPOINT_OUT)
assert ep is not None

while(1):
    ep.write(0x5553424350DDBC880000000000000600000000000000000000000000000000)
    ep.write(0x5553425350ddbc880000000000)

Solution

  • In both cases, the error is:

    Traceback (most recent call last):
      File "D:\py\usb.py", line 1, in <module>
    

    which means it has file usb.py in PATH earlier (probably in . which is D:\py\ in this case) than the path to the Python modules.

    Did you install this module properly? Try rename this usb.py file to something else, you'll see if the error becomes "ImportError: No module named usb". Also check Python install path (something like C:\Python27\) for usb folder i.e. <python_path>\lib\site-packages\usb\core.py.