1

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'
  • 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 '17 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 '17 at 4:03
6

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.

  • 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.) – Steven Spark Sep 20 '18 at 10:56
  • This worked for me. – niceguydave Jul 3 at 15:59
1

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.