XBee Am getting ImportError: No Module named serial?? via Python on Raspberry Pi

2,220 views
Skip to first unread message

Dave Brown

unread,
Mar 1, 2014, 10:46:37 PM3/1/14
to pytho...@googlegroups.com
I am confounded by this as I can't picture what I am doing incorrectly.  I am using Charles Bell book to help get started with network construction.
I am breaking down code into segments and running each segment before adding.

I am stuck here:

import serial
from xbee import xbee
SERIAL_PORT= '/dev/tty/AMA0'
BAUD_RATE=9600

ser_port=serial.Serial(SERIAL_PORT,BAUD_RATE)

Running this segment returns the "No Module name serial" error as mentioned.



Dave Brown

unread,
Mar 1, 2014, 11:54:23 PM3/1/14
to pytho...@googlegroups.com
Just to add, I downloaded the Python Protocol handlers to my MacBook and tried to run the Alarm.py example and I got the no module named serial error on the Mac as well.

Paul Malmsten

unread,
Mar 1, 2014, 11:57:53 PM3/1/14
to pytho...@googlegroups.com
python-xbee depends upon the PySerial library (http://pyserial.sourceforge.net/); install it and try running your program again.

Regards,
~Paul Malmsten


--
You received this message because you are subscribed to the Google Groups "Python XBee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-xbee...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kevin Glavin

unread,
Mar 1, 2014, 11:58:23 PM3/1/14
to pytho...@googlegroups.com
Have you installed pyserial?

pip install pyserial

Sent from Mailbox for iPhone


On Sat, Mar 1, 2014 at 11:54 PM, Dave Brown <dvbr...@gmail.com> wrote:

Dave Brown

unread,
Mar 2, 2014, 9:56:23 AM3/2/14
to pytho...@googlegroups.com
So. to test the Import Serial operation I downloaded pyserial and I believe that I utilized " easy install -U " method.  I ran a sample from the library and stil got the same "No module named serial" message.  Where should the install and setup take place, what directory should the install/setup be completed in?  I have it in the xbee 2.1.0/example directory on my Mac as that is where the program that I am testing is located.  Perhaps I should have installed into a root. Directory?   It was my impression that the imported file needed to be in the same location as the program doing the importing?  To re-iterate I am doing the testing on my Mac but my intention is to run the sensor network from a RPi/Arduino platform.

Thanks,

Dave

Kevin Glavin

unread,
Mar 2, 2014, 10:25:38 AM3/2/14
to pytho...@googlegroups.com
Hi Dave,

As long as it's installed somewhere in your Python path, it doesn't matter what directory you installed PySerial in. When you run easy_install, it should be installing by default to your Python's site-packages directory. There are any number of things that can be going on here. I'm going to assume that you're new to Python based on the nature of your question, so I apologize if these steps are too elementary (leading $ symbols denote command line entry in shell):

1. Verify the version of Python that you're using
        $ python --version
2. Verify that PySerial is installed correctly to your site-packages (e.g., '/Library/Python/2.7/site-packages' where 2.7 is the appropriate version from step 1)
3. Run the python interpreter in interactive mode
        $ python
4. import serial and verify it is the expected version
        >>> import serial
        >>> serial
        You should receive output like: 
            <module 'serial' from '/Library/Python/2.7/site-packages/pyserial-2.7-py2.7.egg/serial/__init__.pyc'>

My assumption is that one of these steps >= 2 is going to fail. Let us know what the output of the failure message is.

Also, if you'd like to see your Python path:
    $ python
    >>> import sys
    >>> sys.path

If this is the only thing that you're going to be doing with Python, keep on in the manner, however, if you plan to get more involved in python programming you'll want to look into using virtual environments. My preference is virtualenvwrapper.
--
Kevin Glavin

Dave Brown

unread,
Mar 2, 2014, 11:03:46 AM3/2/14
to pytho...@googlegroups.com
Kevin,

I am new to Python, so I take no offense to anything, I appreciate your help.
The following is a transcript of my most recent actions:

avebrown$ easy_install -U pyserial
Searching for pyserial
Best match: pyserial 2.7
Processing pyserial-2.7-py2.6.egg
pyserial 2.7 is already the active version in easy-install.pth
Installing miniterm.py script to /usr/local/bin

Using /Library/Python/2.6/site-packages/pyserial-2.7-py2.6.egg
Processing dependencies for pyserial
Finished processing dependencies for pyserial
Dave-Browns-MacBook-Pro:~ davebrown$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> serial
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'serial' is not defined
>>> import sys
>>> 

I was pretty sure that this would work but again I was incorrect.

Dave

Kevin Glavin

unread,
Mar 2, 2014, 11:08:11 AM3/2/14
to pytho...@googlegroups.com
so the immediate problem with that bit of script is that you didn't import serial first. Any module that isn't a built-in in needs to be imported.

import serial
serial

should show the expected behavior, if not there's something else going on.

Dave Brown

unread,
Mar 2, 2014, 11:25:30 AM3/2/14
to pytho...@googlegroups.com
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named serial
>>> 

Sorry but I did try it, I just missed it in the cmd -V  see the above.

Paul Malmsten

unread,
Mar 2, 2014, 11:35:41 AM3/2/14
to pytho...@googlegroups.com
Do you have multiple versions of Python installed? I saw from your previous easy_install output:

...
Using /Library/Python/2.6/site-packages/pyserial-2.7-py2.6.egg
...

Which mentions Python 2.6. However, the Python you run from the terminal reports Python 2.7.3

You could try explicitly using the python version from the terminal (2.7.3) by running:

python -m easy_install pyserial

Does that help?

~Paul Malmsten

Kevin Glavin

unread,
Mar 2, 2014, 11:42:29 AM3/2/14
to pytho...@googlegroups.com
EDIT: I just saw Paul's response to the thread separately, and I completely missed the versioning issue, follow Paul's suggestion first. If you're going to work on multiple python projects, I still recommend using virtualenvwrapper as below so that you can encapsulate your python environments on a per project basis.

what I'm about to suggest is overkill, no bones about it, but will ensure that you have a fresh and working environment to start from.

easy_install pip
pip install virtualenvwrapper
export $WORKON_HOME=~/.envs
mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh
mkvirtualenv xbee

At this point, virtualenvwrapper should create a virtual environment that contains python and pip, and your command prompt should be prepended with '(xbee)'. If you don't see the '(xbee)' text at the beginning of the command prompt, run:

workon xbee

This will wrap your shell with the virtual environment. At this point, you'll want to install all of the dependencies that you need again, and also ensure that you can import serial since it's been problematic:

(xbee) $ pip install pyserial
(xbee) $ python
>>> import serial
>>> serial
... output from serial ...
>>> quit()
(xbee) $ python /path/to/XBee/setup.py install
etc.

While this won't fix your native python environment, it should get you up and running for a quick test. Note that if you want to have virtualenvwrapper available from any command terminal that you open you'll need to add the following lines to ~/.bash_profile:

export WORKON_HOME=~/.envs
source /usr/local/bin/virtualenvwrapper.sh

Dave Brown

unread,
Mar 2, 2014, 11:44:44 AM3/2/14
to pytho...@googlegroups.com
I am not able to use easy intall and I do have multiple versions of Python Intalled, as far as I know I only Use 3.X and 2.7.3
Here is a transcript of your last suggestion, and thanks by the way!

Dave-Browns-MacBook-Pro:~ davebrown$ python -m easy_install pyserial
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named easy_install

Dave Brown

unread,
Mar 2, 2014, 12:12:03 PM3/2/14
to pytho...@googlegroups.com
Tried it again, 

Still no go:
Dave-Browns-MacBook-Pro:~ davebrown$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named serial
>>> 

I think that I am going to give up on the Mac install and move to the Raspberry Pi starting fresh.
I think there is too much Mac baggage on this computer.

If you think of anything else let me know.



On Saturday, March 1, 2014 9:57:53 PM UTC-7, Paul Malmsten wrote:

Dave Brown

unread,
Mar 2, 2014, 12:18:06 PM3/2/14
to pytho...@googlegroups.com
The vitualenvwrapper sounds cool everything was going OK until it got close to the end and then there appeared this appeared see the end of transcript.

Traceback (most recent call last):


  File "<string>", line 17, in <module>

  File "/private/var/folders/op/opC4zUimEmK+3LeaThEEVk+++TI/-Tmp-/pip_build_davebrown/virtualenvwrapper/setup.py", line 7, in <module>

    pbr=True,

  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/core.py", line 113, in setup

    _setup_distribution = dist = klass(attrs)

  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/setuptools/dist.py", line 223, in __init__

    _Distribution.__init__(self,attrs)

  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/dist.py", line 270, in __init__

    self.finalize_options()

  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/setuptools/dist.py", line 256, in finalize_options

    ep.load()(self, ep.name, value)

  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py", line 1907, in load

    entry = __import__(self.module_name, globals(),globals(), ['__name__'])

ImportError: No module named core

----------------------------------------

Cleaning up...

Command python setup.py egg_info failed with error code 1 in /private/var/folders/op/opC4zUimEmK+3LeaThEEVk+++TI/-Tmp-/pip_build_davebrown/virtualenvwrapper

Storing debug log for failure in /Users/davebrown/Library/Logs/pip.log

Dave-Browns-MacBook-Pro:~ davebrown$ 

Kevin Glavin

unread,
Mar 2, 2014, 12:24:37 PM3/2/14
to pytho...@googlegroups.com
Yeah, something is hosed with your core python install. If you're a Mac power user, I recommend trying to redo your python install with a full-featured package manager like Homebrew. Truly, one of the biggest shortcomings of OSX is not having a native package manager like brew. I'd be happy to help outside of the main thread here, and when we come to a working solution, we can post it back to the thread for posterity should anyone else end up in the same boat.

Sent from Mailbox for iPhone


Dave Brown

unread,
Mar 2, 2014, 12:37:54 PM3/2/14
to pytho...@googlegroups.com
Kevin,

That would be awesome.  I will definitely go along with that.  I was trying to debug the whole serial to Xbee to Arduino to Raspberry Pi connection with my Mac.  I had to have multiple versions of Python on my Mac because I was taking two simultaneous courses that used disparate Versions.  Gack!  So as a consequence I did some hosing.
Perhaps you could provide me with the first couple of steps when you have time, and I can follow along.  I will document the effort for posterity and hopefully further posts.
Dave
Reply all
Reply to author
Forward
0 new messages