1

I was looking for some documetation on pyusb . Found this link and tried to use Lennart Renegro's answer.

import usb in python shell on IDLE gave me the following error:

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:

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):

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?

Community
  • 1
  • 1
Flame of udun
  • 2,136
  • 7
  • 35
  • 79
  • Are you sure the python version of IDLE and your bash shell are the same? – chtenb Mar 01 '14 at 13:39
  • @Chiel92 I'm not sure. The first line of python shell says Python 3.3.2+ (default, Oct 9 2013, 14:50:09) [GCC 4.8.1] on linux. I don't know the version of my bash shell and don't know how to check it either. Is there a command I can run to see that? – Flame of udun Mar 01 '14 at 13:42
  • In your shell run: `>>> import sys >>> print (sys.version)` – chtenb Mar 01 '14 at 13:46
  • Probably you meant to run the script on IDLE. Afterall we know the version of the Python used in the Linux shell already, and we just need to compare it to the version in IDLE. – justhalf Mar 01 '14 at 13:51
  • Oh ok. python shell- 3.3.2+ (default, Oct 9 2013, 14:50:09) [GCC 4.8.1] – Flame of udun Mar 01 '14 at 13:51
  • No I can already see it printed as an auto generated text. I was inquiring about bash but according to @justhalf that is already known – Flame of udun Mar 01 '14 at 13:55
  • I'm confused. Can you run the `import sys; print sys.version` on IDLE and post the result? The one you posted already is for the Python in Linux shell, right? The one that you get after running `python` in bash, right? I assume "Python shell" refers to the one in bash. Is it not the case? – justhalf Mar 01 '14 at 14:03
  • @justhalf No that one is for the python in IDLE. Sorry for creating the confusion. I thought python shell referred to IDLE. – Flame of udun Mar 01 '14 at 14:07
  • So then run `python` on your Linux shell, it will show you the Python version. – justhalf Mar 01 '14 at 14:08
  • @justhalf Linux shell - Python 2.7.5+ (default, Sep 19 2013, 13:48:49) [GCC 4.8.1] on linux2 – Flame of udun Mar 01 '14 at 14:10
  • So they are different, as @Chiel92 has suspected. You may want to change the version in your IDLE to match the one in Linux shell. – justhalf Mar 01 '14 at 14:13
  • See this post: http://stackoverflow.com/questions/4776359/python-idle-change-python-version – justhalf Mar 01 '14 at 14:15

1 Answers1

0

Summarizing the discussion above, apparently the python versions of IDLE and the default python shell are different. You installed pyusb for python 2.7.5, while you want to use it on IDLE which runs python 3.3.2.

To fix this problem, either

  • install pyusb for python 3.3.2
  • make sure to use python 2.7.5 with IDLE. For the latter you can have a look at the question @justhalf pointed to: Python IDLE: Change Python Version
Community
  • 1
  • 1
chtenb
  • 14,924
  • 14
  • 78
  • 116
  • On the official page it says pyusb runs on any python version >= 2.4 – Flame of udun Mar 01 '14 at 15:26
  • That is correct, but installing some package for python 2.7.5 doesn't make it available for python 3.3.2 automatically. You have to install the *same package* for 3.3.2 separately. – chtenb Mar 01 '14 at 15:59