3

I am doing a project where I want to pass information from an Arduino to a Raspberry Pi via USB. I've been looking at http://blog.makezine.com/2012/10/22/a-halloween-sound-trigger-with-raspberry-pi-and-arduino/ and Reading serial data from Arduino. I have downloaded python-serial and import serial works in python v2.7 but not v3.2

I've written a .py file but it seems it uses v3.2 Does anyone know how I can force it to run using v2.7?

Thanks

I have since tried what Goldilocks suggested.

  • using #!/usr/bin/python2.7 at the start of the script
  • running it as python2.7 firsttest.py

but to no avail. I can still use import serial in the Python shell with no error.

The error message in full:

Traceback (most recent call last):

File "firsttest.py", line 4, in < module >

import serial

ImportError: No module named serial

I should also point out that thanks to Goldilocks I have confirmed I am using python2.7 so there is a different problem. Could it be that the commandline looks for installed modules in a different location?

Thanks again.

3 Answers 3

3

Turns out that for some reason sudo apt-get install python-serial was not sufficient to download the package. So while I could import it to Python Shell when I ran a script from the command line it couldn't find the module in /usr/bin/

If anyone else has this problem try downloading the pyserial-x.y.tar.gz from pypi. Then unzip using tar xfz pyserial-x.y.tar.gz enter the new directory cd ~/pyserial-x.y and finally run sudo python setup.py install

Thank you Goldilocks for your help in debugging!

It appears that pip installs the modules in a dist-packages folder because it assumes you are using Debian. This is fine for when you want to write some code in the IDE. However, when you run a script from the command line and it doesn't seem to search the dist-packages folder. Therefore, you need to somehow link/move the library to site-packages. (/usr/local/lib/python2.7/site-packages). When I set up my Raspberry Pi I downloaded the Raspbian Wheezy image so those that used a different version may not have this problem.

2
  • If you are having problems installing python packages using apt you could try using easy_install or pip. As these are dedicated Python package managers they will be more likely to ensure that all the correct links are put into the right places. I've always been a bit skeptical of using aptitude to install python modules.
    – Will
    Jul 12, 2013 at 23:30
  • @Will it was through using pip that I ended up in this mess. I'm now having the exact same problem with numpy after using sudo pip install numpy --upgrade.
    – E. Lee
    Jul 30, 2013 at 14:35
1

Reference the specific version of python you want to use in the shebang. /usr/bin/python is a symlink. On my raspbian pi it's to python 2.7:

> stat /usr/bin/python
  File: `/usr/bin/python' -> `python2.7'

Evidently your's is to 3.2, but presuming 2.7 exists, you can just use:

#!/usr/bin/python2.7

as the shebang opening the script.

whereis python will show you all the available pythons.

You can also specify the specific interpreter on the command line:

> python2.7 myprogram.py
4
  • 1
    Thanks. Unfortunately it turns out it's already using v2.7 automatically. I had assumed it was using v3.2 because within the Python Shell v2.7 import serial works but in Python Shell v3.2 import serial threw the same error as the command line does.
    – E. Lee
    Jul 11, 2013 at 12:56
  • Sorry for the confusion: I've written a .py file not a script. Edited in the question.
    – E. Lee
    Jul 11, 2013 at 12:58
  • 1
    @E.Lee A ".py" file is a python script. Use the shebang (it must be the first line), make the file executable, and run it ./whatever.py. Alternately, you can specify the interpreter on the command line, see my last paragraph.
    – goldilocks
    Jul 11, 2013 at 15:10
  • Ah. I tried the shebang (on the first line). No luck - same error. I also tried python2.7 with the same result. Also I'm used to programming in C so didn't realise the nomenclature. Apologies. It's really odd because I can do all these things in the Python Shell and they work!
    – E. Lee
    Jul 11, 2013 at 15:17
0

I had the same problem and tried

sudo apt-get install python3-serial

(note the 3) and that did the trick. Obviosly my previous

sudo apt-get install python-serial 

only installed the 2.7 version of the pyserial module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.