Install pyserial, not serial. The package is named pyserial but imported as serial.
What the message means
A different package called serial exists and breaks the import. Also, pip may install for a different Python than the one you run.
Fix it in this order
- Uninstall the package named serial if it is installed.
- Install pyserial with pip for the same Python you run.
- Use python -m pip to be sure the versions match.
- Check the import with a one-line test.
- Use a virtual environment for each project.
- Check the port name and permission afterwards.
Why it happens
The module imported as serial is provided by the package pyserial. Installing a package named serial gives a different library that does not work. The error also appears when pip installed the package for a different Python from the one running your script.
Run pip uninstall serial, then pip install pyserial, and use the same interpreter for both, for example python -m pip install pyserial.
If you see this, do this
| You see | Do this |
|---|---|
| Module not found | Package missing for this Python. |
| Has no attribute Serial | Wrong serial package. |
Related
serial permission, Serial Monitor blank.
Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.
Why this happens
Python finds a module by name on its search path, and the name you import is not always the name you install. The serial-port library is published on PyPI as pyserial, but the module inside it is called serial. A different project that is called serial exists too. If it is installed, import serial finds that one first, and you get errors like module has no attribute Serial.
The other common cause is two Pythons on one machine. The pip command may belong to Python 3.12 while your IDE runs 3.10, so the package is installed, but not for the interpreter that is running. A file of your own named serial.py in the script folder also shadows the real module, because the current folder is searched first.
How to confirm the cause
- Print which Python is running: python -c “import sys; print(sys.executable)”. Use that exact interpreter for pip.
- Run python -m pip show pyserial. If it says not found, pyserial is not installed for that Python.
- Run python -m pip list and look for a package named serial. If both are listed, remove serial.
- Print serial.__file__ after importing. A path inside your project folder means your own serial.py or a serial folder is shadowing the library.
- In an IDE, check the selected interpreter (virtual environment) in its settings.
Worked example
Run these in a terminal, then use the short script. Replace COM3 with your port.
python -m pip uninstall serial
python -m pip install pyserial
python -c "import serial; print(serial.__version__)"
# then in your script:
import serial
ser = serial.Serial("COM3", 9600, timeout=1)
print(ser.readline())
ser.close()Common mistakes
- Running pip install serial. The command succeeds but installs the wrong library.
- Calling pip while a virtual environment is not active, so the package lands in the system Python.
- Naming your own script serial.py or leaving a serial.pyc or serial folder next to it.
- Assuming the error is about permission or the port. A missing module fails at the import line, before any port is opened.
If this did not fix it
- Check the port name: COM3 and similar on Windows, /dev/ttyUSB0 or /dev/ttyACM0 on Linux.
- Check that no other program, such as a serial monitor, holds the port open.
- On Linux, check that your user is in the dialout group if you get a permission error.
- Check the baud rate against the sketch.