Python AttributeError: 'module' object has no attribute 'Serial'


I'm trying to access a serial port with Python 2.6 on my Raspberry Pi running Debian. My script named tries to import pySerial:

import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.write("hello world!")

For some reason it refuses to establish the serial connection with this error:

AttributeError: 'module' object has no attribute 'Serial'

When I try to type the same code in the interactive Python interpreter it still doesn't work.

Strangely, it used to work about a couple hours ago.

What could be the problem? I've tried to fix this for a while, installing pySerial again, rewriting my code, double-checking the serial port, etc.

Thanks in advance!

This question is tagged with python serial-port raspberry-pi

~ Asked on 2012-07-09 22:09:25

The Best Answer is


I'm adding this solution for people who make the same mistake as I did.

In most cases: rename your project file '' and delete serial.pyc if exists, then you can do simple 'import serial' without attribute error.

Problem occurs when you import 'something' when your python file name is ''.

~ Answered on 2012-08-08 21:19:10


I accidentally installed 'serial' (sudo python -m pip install serial) instead of 'pySerial' (sudo python -m pip install pyserial), which lead to the same error.

If the previously mentioned solutions did not work for you, double check if you installed the correct library.

~ Answered on 2017-10-30 13:27:01

Most Viewed Questions: