171

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.

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
hao_maike
  • 2,929
  • 5
  • 26
  • 31

7 Answers7

221

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'.

Koedlt
  • 4,286
  • 8
  • 15
  • 33
sql
  • 2,219
  • 2
  • 12
  • 2
206

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.

Kevin
  • 2,760
  • 3
  • 15
  • 30
  • 12
    And the fix is to uninstall both, then reinstall pyserial. – jcaron Feb 20 '18 at 17:39
  • 21
    Thank you, uninstall serial with `pip uninstall serial` fixed my problem. Then installed pyserial `pip install pyserial` – Dardan Iljazi Mar 16 '18 at 15:07
  • 6
    Additionally, `pip install --upgrade --force-reinstall pyserial` might help after removing `serial` (or `pip3 ...`). – handle Dec 04 '19 at 09:14
  • Avoid `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](https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe) – jozxyqk Dec 13 '21 at 07:38
137

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.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
VGO
  • 2,161
  • 2
  • 17
  • 14
  • 3
    I had the same problem several times while importing modules. I don't understand why it does work in some cases (for instance when you look at some examples in the [*serial* website](http://pyserial.sourceforge.net/shortintro.html)) – VGO Jul 09 '12 at 22:39
  • 38
    I tried. not work. The error will be "ImportError: cannot import name serial" – Zhang LongQI May 12 '14 at 10:10
  • 23
    This looks utterly wrong, sorry. At first the class is `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. – erikbstack Apr 16 '16 at 08:22
  • This does not solve the problem described, and should not have been accepted. It is perfectly okay to import the module rather than the class, because the code then explicitly accesses the class from the module. If the class were imported directly, the rest of the code would have to be modified to match. And again, this *does not change the fact that naming the file `serial.py` causes a problem*. – Karl Knechtel Aug 01 '22 at 06:50
  • @Kevin the names that PyPI uses for packages do not need to, and often do not, match the "package name" as understood by Python. It is *not a matter of opinion*. The package that you get by using `pip install pyserial`, requires code that says `import serial`, and will not work with code that says `import pyserial`. – Karl Knechtel Aug 01 '22 at 06:51
47

You have installed the incorrect package named 'serial'.

  • Run pip uninstall serial for python 2.x or pip3 uninstall serial for python 3.x
  • Then install pyserial if not already installed by running pip install pyserial for python 2.x orpip3 install pyserial for python 3.x.
FutureJJ
  • 2,368
  • 1
  • 19
  • 30
2

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...

Community
  • 1
  • 1
Nearoo
  • 4,454
  • 3
  • 28
  • 39
  • It's not clear how this is supposed to solve the problem. We list the modules in the package... and then what? What are "the problems", and *how* were they solved? And how does this relate to the *specific* problem *described in the OP*? – Karl Knechtel Aug 01 '22 at 06:58
  • @KarlKnechtel This was 8 years ago, I don't remember. But I can read my answer for you: How? No idea. How does this relate to the topic? It solved his/her problem (getting rid of an error message), if it was the same one I had, which appeared to me to be the case. – Nearoo Aug 02 '22 at 10:47
1

This error can also happen if you have circular dependencies. Check your imports and make sure you do not have any cycles.

Chad Zawistowski
  • 1,876
  • 1
  • 13
  • 16
1

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

Sources : https://raspberrypi.stackexchange.com/questions/74742/python-serial-serial-module-not-found-error/85930#85930

Tryhard

Benjamin
  • 100
  • 11