【问题标题】:Python shell says no module named usb but linux shell doesn'tPython shell 说没有名为 usb 的模块,但 linux shell 没有
【发布时间】: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 中运行:&gt;&gt;&gt; import sys &gt;&gt;&gt; print (sys.version)
  • 可能您打算在 IDLE 上运行脚本。毕竟我们已经知道 Linux shell 中使用的 Python 的版本,我们只需将其与 IDLE 中的版本进行比较即可。
  • 哦,好的。 python shell- 3.3.2+(默认,2013 年 10 月 9 日,14:50:09)[GCC 4.8.1]

标签: pyusb python


【解决方案1】:

总结上面的讨论,显然IDLE的python版本和默认的python shell是不同的。您为 python 2.7.5 安装了pyusb,而您想在运行 python 3.3.2 的 IDLE 上使用它。

要解决这个问题,要么

【讨论】:

  • 在官方页面上说 pyusb 可以在任何 python 版本 >= 2.4 上运行
  • 这是正确的,但是为 python 2.7.5 安装一些包并不能自动使其可用于 python 3.3.2。您必须为 3.3.2 单独安装 same package
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2011-11-14
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多