Skip to main content

How to Solve "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 (or python-serial) instead of the correct pyserial package. These other packages exist on PyPI, but they are not the serial communication library you need.
  • Shadowing: You have a file named serial.py in your project directory (or a directory in your Python path). Python's import system prioritizes local files, so your serial.py is being imported instead of the pyserial library.

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 the serial module. You should see Serial (uppercase 'S') in the output. If you don't, you're still importing the wrong module.
  • serial.__file__: This shows the full path to the serial module that's being imported. It should point to a location within your Python installation's site-packages directory (or your virtual environment's site-packages directory), 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.