python pyusb import usb.core doesn't work
Asked Answered
A

4

10

I am following this tutorial (http://pyusb.sourceforge.net/docs/1.0/tutorial.html)

I am on windows xp sp3, my python version is 2.7 and I downloaded and installed the pyusb-1.0.0-a1.zip

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

p.s. "from usb import core" this make

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

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 config. with no args, the first config will be the active one

dev.set_configuration()

#get an end point instance
ep = usb.util.find_descriptor(
    dev.get_interface_altsetting(), #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)
Adolescence answered 2/6, 2011 at 10:32 Comment(0)
U
4

In both cases 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 path to 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.

Uteutensil answered 2/6, 2011 at 10:40 Comment(4)
i change the directory to desktop, name to usb2.py it says Traceback (most recent call last): File "C:/Documents and Settings/kty1104/Desktop/usb2.py", line 3, in <module> dev = usb.core.find(idVendor=0x1516, idProduct=0x8628) NameError: name 'usb' is not definedAdolescence
thanks it works fine now is it normal that "import usb" doesn't work but "import usb.core" works?Adolescence
Try open interactive Python shell (or even better install and use IPython) and do: import usb, next help(usb) (or usb? in IPython), find line FILE and check if usb module is in given path (shold be a directory called usb* with at least init.py file and core.py file).Dunaway
And yet, after 6 years your answer help me understand how to install modules in python. Thanks! :) #44563598Schafer
F
12

Your question says you're using 1.0, but I had the same symptoms as you did, so I'll put this here for future search-engine users.

If you can import usb but not import usb.core you may be running python-usb 0.x instead of 1.0.

https://github.com/walac/pyusb

Flavorsome answered 20/2, 2012 at 8:38 Comment(0)
U
4

In both cases 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 path to 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.

Uteutensil answered 2/6, 2011 at 10:40 Comment(4)
i change the directory to desktop, name to usb2.py it says Traceback (most recent call last): File "C:/Documents and Settings/kty1104/Desktop/usb2.py", line 3, in <module> dev = usb.core.find(idVendor=0x1516, idProduct=0x8628) NameError: name 'usb' is not definedAdolescence
thanks it works fine now is it normal that "import usb" doesn't work but "import usb.core" works?Adolescence
Try open interactive Python shell (or even better install and use IPython) and do: import usb, next help(usb) (or usb? in IPython), find line FILE and check if usb module is in given path (shold be a directory called usb* with at least init.py file and core.py file).Dunaway
And yet, after 6 years your answer help me understand how to install modules in python. Thanks! :) #44563598Schafer
E
4

I suppose that "D:\py\usb.py" is the name of your py test program.

Unfortunately this make confusion to the py compiler due to the fact that usb is also the name of the module.

Change it in usbtest.py and everything works

Epicedium answered 23/10, 2013 at 12:19 Comment(1)
I was going to answer this , Thanks for mentioning itKimmi
K
2

for understanding where python looks to import your module, you can run following code :

import sys
print(sys.path)

this will show you list of directory names that python searches for your module to import :)

Kimmi answered 26/4, 2014 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.