3

I am trying to connect to the serial ports a Raspberry Pi 3 Model B running Raspbian through Python.

import serial works fine but ser = serial.Serial("dev/ttyAMA0", baudrate=9600) yields this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Serial'
2
  • 1
    Have you created a file called serial.py at any point and put it in an import location (e.g. your current directory)? That would likely break things. Also, how did you install pySerial?
    – Aurora0001
    Nov 4, 2017 at 18:12
  • No. I there is no file named serial.py at any point and I have deleted every .pyc file and used from serial import serial as I am importing the module not the class still getting the error.
    – Tapas
    Nov 5, 2017 at 4:03

4 Answers 4

9

First uninstall serial with sudo pip uninstall serial (thank you so much Ali!)

Then, if import serial does not work anymore: use sudo pip install pyserial. This will install the correct serial module for the RP.

3
  • BaxMode's answer helped me. (but I cannot up vote it yet due to lack of reputation here) I pip installed serial, when I should have installed pyserial. (I'm using Windows with Anaconda, so conda install pyserial worked for me.) Sep 20, 2018 at 10:56
  • This worked for me. Jul 3, 2019 at 15:59
  • This works, but I'll add a bit more explanation. There are two different packages that provide a module called 'serial' to the python system. One is called 'serial' by pip, and that is a serialization library (nothing to do with serial ports). The other is called 'pyserial' by pip, and that is the one that does serial port handling on the system. It is a very unfortunate name conflict in the python ecosystem, that I've hit myself. Uninstalling the serialization module and making sure the serial port handling module is installed should work.
    – Tim Bird
    Aug 23, 2021 at 17:09
2

I had the same issue and I tried all the suggestions I encountered on the internet, but non has worked for me. Finally, I was able to solve the problem by uninstalling the serial package from usr/local/lib that had the issue for some reason. you can uninstall this package by sudo pip uninstall serial. Here is what you can try:

  • write this short piece of code import serial print(serial.__file__),. This outputs the path of the module being imported in your code.
  • if the output is /usr/local/lib/pythonx.x/dist-packages/serial/__init__.py, then go ahead and uninstall serial. If it was /usr/lib/pythonx.x/dist-packages/serial/__init__.py, then you have another type of problem.
  • repeat the 1st step after the uninstall and check that the output is the second one
  • now try your code again

PS: you may need to use pip3 instead of pip in case you are using python3.

Hope this helps

0

have a look at this page. I have the same issue because I have installed serial instead of pyserial. To check, enter dir(serial) If you do not see the Serial method, you have the wrong package

-1

Yes this topic is one year 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

Activating __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

Sources : Tryhard

Not the answer you're looking for? Browse other questions tagged or ask your own question.