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.
7 Answers 7
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.
4 Comments
pip uninstall serial fixed my problem. Then installed pyserial pip install pyserialpip install --upgrade --force-reinstall pyserial might help after removing serial (or pip3 ...).sudo pip.... This will often create files only readable by root and break your python install. Moreover it clutters your global environment. Either pip install --user or use a virtual environment I'm adding this solution for people who make the same mistake as I did.
In most cases: rename your project file serial.py and delete serial.pyc if exists, then you can simply do import serial without the attribute error.
Problem occurs when you import 'something' when your python file name is 'something.py'.
8 Comments
AttributeError: 'module' object has no attribute 'SerialExceptionserial :-)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.
5 Comments
Serial not serial, then you don't have to import a class to use it. module.class() should work very fine. Last but not least there is no real explanation for what's going on here at all.serial.py causes a problem.pip install pyserial, requires code that says import serial, and will not work with code that says import pyserial.You have installed the incorrect package named 'serial'.
- Run
pip uninstall serialfor python 2.x orpip3 uninstall serialfor python 3.x - Then install pyserial if not already installed by
running
pip install pyserialfor python 2.x orpip3 install pyserialfor python 3.x.
Comments
If you are helpless like me, try this:
List all Sub-Modules of "Serial" (or whatever package you are having trouble with) with the method described here: List all the modules that are part of a python package
In my case, the problems solved one after the other.
...looks like a bug to me...
2 Comments
This error can also happen if you have circular dependencies. Check your imports and make sure you do not have any cycles.
Comments
Yes this topic is a bit old but i wanted to share the solution that worked for me for those who might need it anyway
As Ali said, try to locate your program using the following from terminal :
sudo python3
import serial
print(serial.__file__) --> Copy
CTRL+D #(to get out of python)
sudo python3 -->paste/__init__.py
Running __init__.py will say to your program "ok i'm going to use Serial from python3". My problem was that my python3 program was using Serial from python 2.7
Other solution: remove other python versions
Cao
Tryhard
1 Comment
__init__.py file.
from serial import serial?