How to Resolve "AttributeError: module 'serial' has no attribute 'Serial'" in Python
The error AttributeError: module 'serial' has no attribute 'Serial' when working with serial communication in Python typically indicates a naming conflict. You've likely installed the wrong package, or a local file is shadowing the pyserial module.
This guide explains the causes and provides a clear, step-by-step solution.
Understanding the Error: Package and Naming Conflicts
The pyserial library provides the serial module (lowercase 's'), which contains the Serial class (uppercase 'S'). The error message indicates that the serial module you're actually importing doesn't have a Serial class. This happens in two main scenarios:
- Wrong Package: You might have installed a package named
serial(orpython-serial) instead of the correctpyserialpackage. These other packages exist on PyPI, but they are not the serial communication library you need. - Shadowing: You have a file named
serial.pyin your project directory (or a directory in your Python path). Python's import system prioritizes local files, so yourserial.pyis being imported instead of thepyseriallibrary.
Solution: Installing pyserial and Removing Conflicts
Follow these steps to resolve the error:
Uninstalling Incorrect Packages
First, remove any potentially conflicting packages:
pip uninstall serial python-serial pyserial
# OR, for Python 3 (might be pip3, pip3.10, etc.)
pip3 uninstall serial python-serial pyserial
# If pip is not in your PATH:
python -m pip uninstall serial python-serial pyserial
python3 -m pip uninstall serial python-serial pyserial
It's crucial to remove all of these to avoid any lingering conflicts.
Installing pyserial
Now, install the correct package, pyserial:
pip install pyserial
# OR, for Python 3 (might be pip3, pip3.10, etc.)
pip3 install pyserial
# OR, if pip is not in your PATH:
python -m pip install pyserial
python3 -m pip install pyserial
Checking for Local serial.py Files
Thoroughly examine your project directory and any directories in your Python path for a file named serial.py. If you find one, rename it or remove it. This is a very common cause of this error.
Verifying the Correct Module
After installing (and potentially renaming files), verify that you're importing the correct serial module:
import serial
print(dir(serial)) # Should list Serial, among other things.
print(serial.__file__) # Should show a path to .../site-packages/serial/__init__.py
dir(serial): This lists the attributes of theserialmodule. You should seeSerial(uppercase 'S') in the output. If you don't, you're still importing the wrong module.serial.__file__: This shows the full path to theserialmodule that's being imported. It should point to a location within your Python installation'ssite-packagesdirectory (or your virtual environment'ssite-packagesdirectory), and not to a file in your project directory.
If print(serial.__file__) shows a path within your project, you still have a naming conflict. Find and rename that file.
Conclusion
The AttributeError: module 'serial' has no attribute 'Serial' error is usually caused by installing the wrong package or a local file shadowing the correct pyserial module.
By carefully uninstalling incorrect packages, installing pyserial, checking for naming conflicts, and verifying the imported module, you can resolve this error and use the serial communication library successfully.