2014-03-01 24 views
1

我正在尋找pyusb上的一些文檔。發現這link並試圖使用Lennart Renegro的答案。Python shell說沒有模塊命名爲usb但linux shell不是

在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 

這裏發生了什麼?我如何閱讀這些文檔?

+0

你確定IDLE和你的bash shell的python版本是一樣的嗎? –

+0

@ Chiel92我不確定。 Python shell的第一行說Python 3.3.2+(默認,2013年10月9日14:50:09) [GCC 4.8.1]在linux上。我不知道我的bash shell的版本,也不知道如何檢查它。有沒有可以運行的命令來查看? –

+0

在您的shell運行:>>> import sys >>> print(sys.version)' –

回答

0

總結上面的討論,顯然IDLE的Python版本和默認的python shell是不同的。您爲python 2.7.5安裝了pyusb,而您想在運行python 3.3.2的IDLE上使用它。

要解決這個問題,無論是

+0

在官方頁面上,它表示pyusb在任何python版本上運行> = 2.4 –

+0

這是正確的,但是爲python 2.7.5安裝一些軟件包並不會自動爲python 3.3.2提供它。你必須分別爲3.3.2安裝*相同的包*。 –