简体   繁体   English

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

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

I was looking for some documetation on pyusb . 我正在寻找有关pyusb的文档。 Found this link and tried to use Lennart Renegro's answer. 找到了此链接,并尝试使用Lennart Renegro的答案。

import usb in python shell on IDLE gave me the following error: 在IDLE的python shell中import usb给了我以下错误:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import usb
ImportError: No module named 'usb'

However, I ran this program using python first.py on bash: 但是,我在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')

and bash return the following error(which I was expecting because I hadn't connected any usb devices atm): 和bash返回以下错误(这是我期望的,因为我没有连接任何USB设备atm):

Traceback (most recent call last):
  File "first.py", line 6, in <module>
    raise ValueError('Device not found')
ValueError: Device not found

What is happening here? 这是怎么回事 And how do I read those docs? 我该如何阅读这些文档?

Summarizing the discussion above, apparently the python versions of IDLE and the default python shell are different. 总结以上讨论,显然IDLE的python版本和默认的python shell不同。 You installed pyusb for python 2.7.5, while you want to use it on IDLE which runs python 3.3.2. 您已为python 2.7.5安装了pyusb ,而您想在运行python 3.3.2的IDLE上使用它。

To fix this problem, either 要解决此问题,

  • install pyusb for python 3.3.2 为python 3.3.2安装pyusb
  • make sure to use python 2.7.5 with IDLE. 确保将python 2.7.5与IDLE一起使用。 For the latter you can have a look at the question @justhalf pointed to: Python IDLE: Change Python Version 对于后者,您可以看看@justhalf指出的问题: Python IDLE:更改Python版本

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

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