AttributeError module 'serial' has no attribute 'Serial'


Borislav Hadzhiev

Wed Apr 20 20222 min read


Photo by Kyler Boone

AttributeError module 'serial' has no attribute 'Serial' #

The Python "AttributeError module 'serial' has no attribute 'Serial'" occurs when we have a local file named and import from the pyserial module. To solve the error, make sure you haven't got a file named, uninstall the serial module and install pyserial.

attributeerror module serial has no attribute serial

First, make sure you haven't installed the serial module instead of pyserial.

# 👇️ uninstall serial and pyserial pip3 uninstall serial pip3 uninstall pyserial # 👇️ install pyserial pip3 install pyserial

Now try importing the serial module.
import serial ser = serial.Serial('/dev/ttyUSB0') print(

Make sure you don't have a local file named

If you have a local module that is named, it would shadow the third-party pyserial module.

Here is an example of how the error occurs in a local file named
import serial ser = serial.Serial('/dev/ttyUSB0') # ⛔️ AttributeError: partially initialized module 'serial' has no # attribute 'Serial' (most likely due to a circular import). Did you mean: 'serial'? print(

The Python interpreter first looks for the imported module in the built-in modules, then in the current directory, then in the PYTHON PATH, then in the installation-dependent default directory.

So, when we create a local file with the same name as that of a third-party module, we effectively shadow the official module with our local file.

It doesn't have to be the file you are running directly. If you have a file anywhere in the directory, it would shadow the official module.

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.
import serial print(serial.__file__) # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/ # ✅ result if pulling in correct module # /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/serial/

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.
import serial # ✅ ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__', 'serial'] print(dir(serial))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the Serial class is not present in the module's attributes, which is a clear indication that we are shadowing the third-party module with our local module.

Make sure you haven't misspelled anything in your import statement as that could also cause the error.

Once you rename your file, you should be able to use the serial module.
import serial ser = serial.Serial('/dev/ttyUSB0') print(
Use the search field on my Home Page to filter through my more than 1,000 articles.