No module named serial
PythonPython Problem Overview
and I got a question when I run my Python code.
I installed Python 2.7 on Windows 7, bit 64. I got an error "No module named serial" when I compiled my code:
import serial
ser = serial.Serial("COM5", 9600)
ser.write("Hello world")
x = ser.readline()
print(x)
I tried many ways to crack this problem, such as installed Canopy to setup virtual environment, make sure 'pip' is there, no Python v 3.x installed. But still cannot get it out.
Any advice would be appreciated.
Python Solutions
Solution 1 - Python
Serial is not included with Python. It is a package that you'll need to install separately.
Since you have pip installed you can install serial from the command line with:
pip install pyserial
Or, you can use a Windows installer from here. It looks like you're using Python 3 so click the installer for Python 3.
Then you should be able to import serial as you tried before.
Solution 2 - Python
You must pip install pyserial
first.
Solution 3 - Python
First use command
pip uninstall pyserial
Then run again
pip install pyserial
The above commands will index it with system interpreter.
Solution 4 - Python
You must have the pyserial library installed. You do not need the serial library.Therefore, if the serial library is pre-installed, uninstall it. Install the pyserial libray. There are many methods of installing:-
pip install pyserial
- Download zip from pyserial and save extracted library in Lib>>site-packages folder of Python.
- Download wheel and install wheel using command:
pip install <wheelname>
Link: https://github.com/pyserial/pyserial/releases
After installing Pyserial, Navigate to the location where pyserial is installed. You will see a "setup.py" file. Open Power Shell or CMD in the same directory and run command "python setup.py install
".
Now you can use all functionalities of pyserial library without any error.
Solution 5 - Python
In my case the command below did the job
pip3 install pyserial
Solution 6 - Python
Download this file :- (https://pypi.python.org/packages/1f/3b/ee6f354bcb1e28a7cd735be98f39ecf80554948284b41e9f7965951befa6/pyserial-3.2.1.tar.gz#md5=7142a421c8b35d2dac6c47c254db023d):
cd /opt
sudo tar -xvf ~/Downloads/pyserial-3.2.1.tar.gz -C .
cd /opt/pyserial-3.2.1
sudo python setup.py install
Solution 7 - Python
sudo apt install python-serial python3-serial
Solved it, using it for esp32
Solution 8 - Python
- Firstly uninstall pyserial using the command
pip uninstall pyserial
- Then go to https://www.lfd.uci.edu/~gohlke/pythonlibs/
- download the suitable pyserial version and then go to the directory where the file is downloaded and open cmd there
- then type pip install "filename"(without quotes)
Solution 9 - Python
I had this same problem multiple times but finally found solution.
I had multiple Python versions installed. Like in Raspberry Pi there was Python3.5 installed and I installed also 3.9.2 without uninstalling 3.5. Then I installed pyserial with pip and tried my program. No module... But the reason was that the linux symbolic link in python3 pointed to python3.9.2 version but pip3 pointed to python3.5. So pyserial was installed in python3.5 and understandably was not found when run python3.9.2. Then I changed symbolic link in pip3 to right version and voila, everything works fine!
Solution 10 - Python
Usually what can happen as was in my case is installing a newer version manually can change the placement of the pyserial from minimal to the upgraded version.
For example, I had just installed Python 3.10 before installing Arduino IDE and that just caused a heck of problems.
I uninstalled pyserial by trying pip uninstall and then
I uninstalled python3.10,
sudo apt purge python3.10
Which after a reboot later, I installed pip install pyserial again, and that did the trick.