python serial port fix error no module named serial - JohnHau/mis GitHub Wiki
AttributeError module 'serial' has no attribute 'Serial'
AttributeError module 'serial' has no attribute 'Serial' The Python "AttributeError module 'serial' has no attribute 'Serial'" occurs when we have a local file named serial.py and import from the pyserial module.
To solve the error, make sure you haven't got a file named serial.py, uninstall the serial module and install pyserial.
# Installing the correct module First, make sure you haven't installed the serial module instead of pyserial.
shell
👇️ uninstall serial and pyserial
pip uninstall serial pip uninstall pyserial
pip3 uninstall serial pip3 uninstall pyserial
👇️ install pyserial
pip install pyserial pip3 install pyserial
Now try importing the serial module.
main.py import serial
ser = serial.Serial('/dev/ttyUSB0')
print(ser.name)
Make sure you don't have a file named serial.py Make sure you don't have a local file named serial.py.
If you have a local module that is named serial.py, it would shadow the third-party pyserial module.
Here is an example of how the error occurs in a local file named serial.py.
serial.py 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(ser.name)
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 serial.py 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.
If your serial import is shadowed by a local file, the result will look something similar to the following.
main.py import serial
print(serial.file)
⛔️ result if shadowed by local file
/home/borislav/Desktop/bobbyhadz_python/serial.py
If your serial import is not shadowed by a local file or variable, the result will look similar to the following.
main.py import serial
print(serial.file)