【发布时间】:2014-03-01 13:32:36
【问题描述】:
我正在寻找一些关于 pyusb 的文档。找到这个link 并尝试使用 Lennart Renegro 的答案。
import usb in python shell on IDLE 给了我以下错误:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import usb
ImportError: No module named 'usb'
但是,我在 bash 上使用 python first.py 运行了这个程序:
import usb
dev = usb.core.find(idVendor = 0xfffe, idProduct = 0x0001)
if dev is None:
raise ValueError('Device not found')
dev.set_configuration()
cfg = dev.get_active_configuration()
interface_number = cfg[(0,0)].bInterfaceNumber
alternate_setting = usb.control.get_interface(interface_number)
intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number, bAlternateSetting = alternate_setting)
ep = usb.util.find.descriptor(intf, custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
assert ep is not None
ep.write('test')
然后 bash 返回以下错误(这是我预料到的,因为我没有连接任何 USB 设备 atm):
Traceback (most recent call last):
File "first.py", line 6, in <module>
raise ValueError('Device not found')
ValueError: Device not found
这里发生了什么?以及如何阅读这些文档?
【问题讨论】:
-
你确定IDLE的python版本和你的bash shell是一样的吗?
-
@Chiel92 我不确定。 python shell 的第一行在 linux 上显示 Python 3.3.2+ (default, Oct 9 2013, 14:50:09) [GCC 4.8.1]。我不知道我的 bash shell 的版本,也不知道如何检查它。有没有我可以运行的命令来查看?
-
在你的 shell 中运行:
>>> import sys >>> print (sys.version) -
可能您打算在 IDLE 上运行脚本。毕竟我们已经知道 Linux shell 中使用的 Python 的版本,我们只需将其与 IDLE 中的版本进行比较即可。
-
哦,好的。 python shell- 3.3.2+(默认,2013 年 10 月 9 日,14:50:09)[GCC 4.8.1]