繁体   English   中英

Python Shell表示没有名为usb的模块,但linux shell没有

[英]Python shell says no module named usb but linux shell doesn't

我正在寻找有关pyusb的文档。 找到了此链接,并尝试使用Lennart Renegro的答案。

在IDLE的python shell中import usb给了我以下错误:

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版本和默认的python shell不同。 您已为python 2.7.5安装了pyusb ,而您想在运行python 3.3.2的IDLE上使用它。

要解决此问题,

  • 为python 3.3.2安装pyusb
  • 确保将python 2.7.5与IDLE一起使用。 对于后者,您可以看看@justhalf指出的问题: Python IDLE:更改Python版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM