python pyusb导入usb.core失败
我正在按照这个教程(http://pyusb.sourceforge.net/docs/1.0/tutorial.html)进行学习。
我使用的是Windows XP SP3,
我的Python版本是2.7,我下载并安装了pyusb-1.0.0-a1.zip
还有libusb-win32-bin-1.2.4.0.zip。
import usb
一切运行得很好。
但是
import usb.core
这个却完全不工作。
它显示
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
有没有解决办法?
谢谢!
附言:
“from usb import core”这行代码导致了
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
完整的源代码在这里
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)
4 个回答
4
我想"D:\py\usb.py"是你测试用的Python程序的名字。
不幸的是,这让Python编译器感到困惑,因为"usb"也是一个模块的名字。
把它改成"usbtest.py",这样一切就能正常工作了。
12
你的问题提到你在使用1.0版本,但我遇到过和你一样的情况,所以我把这个信息放在这里,方便以后通过搜索引擎找到的人。
如果你能成功使用 import usb
但不能使用 import usb.core
,那么你可能是在用python-usb的0.x版本,而不是1.0版本。
4
在这两种情况下,出现的错误是:
Traceback (most recent call last):
File "D:\py\usb.py", line 1, in <module>
这意味着在 PATH
中,usb.py
文件的位置比 Python 模块的路径要早(可能是在 .
这个位置,也就是在这个例子中的 D:\py\
)。
你有没有正确安装这个模块?试着把这个 usb.py
文件改个名字,看看错误信息是否变成 "ImportError: No module named usb"。另外,还要检查一下 Python 的安装路径(像 C:\Python27\
这样的路径),看看里面是否有 usb 文件夹,也就是 <python_path>\lib\site-packages\usb\core.py
这个文件。