3

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

import serial works fine butser = 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'
goldilocks's user avatar
goldilocks
60.4k18 gold badges117 silver badges236 bronze badges
askedNov 4, 2017 at 14:58
Tapas's user avatar
2
  • 1
    Have you created a file calledserial.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?CommentedNov 4, 2017 at 18:12
  • No. I there is no file namedserial.py at any point and I have deleted every .pyc file and usedfrom serial import serial as I am importing the module not the class still getting the error.CommentedNov 5, 2017 at 4:03

4 Answers4

10

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

Then, ifimport serial does not work anymore: usesudo pip install pyserial. This will install the correct serial module for the RP.

answeredJun 10, 2018 at 16:24
BaxMode's user avatar
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, soconda install pyserial worked for me.)CommentedSep 20, 2018 at 10:56
  • This worked for me.CommentedJul 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.CommentedAug 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 theserial package fromusr/local/lib that had the issue for some reason. you can uninstall this package bysudo pip uninstall serial.Here is what you can try:

  • write this short piece of codeimport serialprint(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 uninstallserial. 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 usepip3 instead ofpip in case you are using python3.

Hope this helps

answeredApr 14, 2018 at 8:42
ali's user avatar
0

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

answeredNov 5, 2017 at 20:59
claudio06's user avatar
-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

answeredJul 12, 2018 at 8:37
Benjamin's user avatar
Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.