9

我正在关注教程(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

任何解决方案?

谢谢!

ps“从usb导入核心”这个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

完整的源代码在这里

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 回答 4

12

您的问题说您使用的是 1.0,但我的症状与您相同,因此我将把它放在这里以供将来的搜索引擎用户使用。

如果可以import usb但不是import usb.core,您可能正在运行 python-usb 0.x 而不是 1.0。

https://github.com/walac/pyusb

于 2012-02-20T08:38:18.043 回答
4

在这两种情况下,错误都是:

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

这意味着它比python模块的路径更早(可能在usb.py这种情况下)有文件。PATH.D:\py\

您是否正确安装了此模块?尝试将此usb.py文件重命名为其他文件,您会看到错误是否变为“ImportError:没有名为 usb 的模块”。C:\Python27\还要检查USB 文件夹的Python 安装路径(类似于),即<python_path>\lib\site-packages\usb\core.py.

于 2011-06-02T10:40:06.347 回答
4

我想“D:\py\usb.py”是你的 py 测试程序的名称。

不幸的是,由于 usb 也是模块的名称,这使 py 编译器感到困惑。

在 usbtest.py 中更改它,一切正常

于 2013-10-23T12:19:54.090 回答
2

了解python 在哪里导入您的模块,您可以运行以下代码:

import sys
print(sys.path)

这将显示 python 搜索要导入的模块的目录名称列表:)

于 2014-04-26T12:21:17.153 回答