Python AttributeError: 'module' object does not have 'Serial' attribute

I am trying to access the serial port with Python 2.6 on my Raspberry Pi running Debian. My script called serial.py trying to import pySerial:

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

For some reason, he refuses to establish a serial connection with this error:

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

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

Oddly enough, it worked about a couple of hours ago.

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

Thanks in advance!

+74
python serial-port raspberry-pi
Jul 09 2018-12-12T00:
source share
6 answers

You are importing a module, not a class. So you should write: from serial import serial

+64
Jul 09 '12 at 22:21
source share

I am adding this solution for people who are making the same mistake as me.

In most cases: rename the project file "serial.py" and delete serial.pyc, if it exists, then you can execute a simple "import serial number" without an attribute error.

The problem occurs when you import "something" when your python file name is "something.py".

+124
Aug 08 2018-12-18T00:
source share

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

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

+17
Oct 30 '17 at 13:27
source share

If you are helpless like me, try the following:

List all the "Serial" submodules (or any other package that you come across) using the method described here: List all the modules included in the python package

In my case, the problems are solved one by one.

... looks like a mistake to me ...

+2
Nov 01. '14 at 15:16
source share

This error may also occur if you have circular dependencies. Check your import and make sure you have no loops.

+1
Oct 13 '16 at 18:57
source share

This problem is because your proyect is called serial.py, and the imported library is also a serial number, change the name and that's it.

0
Mar 05 '17 at 17:06 on
source share



All Articles