160

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
  • 3,489
  • 4
  • 25
  • 44
hao_maike
  • 2,786
  • 5
  • 25
  • 30

8 Answers8

214

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 do simple 'import serial' without attribute error.

Problem occurs when you import 'something' when your python file name is 'something.py'.

sql
  • 2,149
  • 2
  • 10
  • 2
179

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,422
  • 3
  • 12
  • 27
  • 11
    And the fix is to uninstall both, then reinstall pyserial. – jcaron Feb 20 '18 at 17:39
  • 17
    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
  • 1
    Hope more people see this, was my mistake as well. Thanks for the help! – Dippy Mar 23 '18 at 21:18
  • Just to add this worked for my Raspberry Pi Zero too. `Linux raspberrypi 4.14.62+ #1134 Tue Aug 14 16:58:07 BST 2018 armv6l GNU/Linux` I'm adding this because I believ(ed) that pyserial was an older module. – Hugh Barnard Aug 27 '18 at 11:18
  • 6
    Additionally, `pip install --upgrade --force-reinstall pyserial` might help after removing `serial` (or `pip3 ...`). – handle Dec 04 '19 at 09:14
  • i am new to python and installed both 'serial' and 'pyserial' , this is the only answer that actully solved. I uninstalled both and then install 'pyserial' , thanks a lot – Feroze Mohamed Feb 27 '20 at 10:20
  • 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
131

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
  • 16,400
  • 3
  • 50
  • 66
VGO
  • 2,095
  • 2
  • 16
  • 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
  • 37
    I tried. not work. The error will be "ImportError: cannot import name serial" – Zhang LongQI May 12 '14 at 10:10
  • 21
    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
  • 3
    This does not solve the problem. – Schütze Jan 21 '19 at 08:56
  • 6
    For me I had run `pip install serial`. Oops, wrong library, meant to install `pyserial`. – geoff May 24 '20 at 06:41
  • I also installed serial before pyserial, but had no issues. When I used pip freeze and pip install -r, serial got installed after pyserial which causes this same error.# – Raphael Oct 29 '20 at 09:21
  • imo if the package is called pyserial, then we should be importing from pyserial not serial – Kevin Jan 07 '22 at 19:42
44

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
  • 1,805
  • 1
  • 17
  • 24
9

This problem is beacouse your proyect is named serial.py and the library imported is name serial too , change the name and thats all.

davidleosam
  • 346
  • 3
  • 4
3

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
  • 3,760
  • 3
  • 28
  • 36
2

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

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 : https://raspberrypi.stackexchange.com/questions/74742/python-serial-serial-module-not-found-error/85930#85930

Tryhard

Benjamin
  • 62
  • 10
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,736
  • 1
  • 11
  • 15