簡體   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