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'- 1Have you created a file called
serial.pyat 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– Aurora00012017-11-04 18:12:46 +00:00CommentedNov 4, 2017 at 18:12 - No. I there is no file named
serial.pyat any point and I have deleted every .pyc file and usedfrom serial import serialas I am importing the module not the class still getting the error.Tapas– Tapas2017-11-05 04:03:24 +00:00CommentedNov 5, 2017 at 4:03
4 Answers4
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.
- 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 pyserialworked for me.)Steven Spark– Steven Spark2018-09-20 10:56:14 +00:00CommentedSep 20, 2018 at 10:56 - 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– Tim Bird2021-08-23 17:09:23 +00:00CommentedAug 23, 2021 at 17:09
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 code
import 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
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
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 serialprint(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
Explore related questions
See similar questions with these tags.
