Search code examples
pythonserial-portraspberry-pi

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 serial.py 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.


Solution

  • You're importing the module, not the class. So, you must write:

    from serial import Serial
    

    You need to install serial module correctly: pip install pyserial.