81

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.

11 Answers 11

118

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.

2
  • 1
    This is the right answer. @Amber.G Please mark it as correct.
    – palsch
    Oct 21, 2015 at 20:10
  • 3
    (for python 3 you can use pip3 install pyserial )
    – IronEagle
    Aug 26, 2020 at 21:01
24

You must pip install pyserial first.

10

First use command

pip uninstall pyserial

Then run again

 pip install pyserial

The above commands will index it with system interpreter.

9

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:-

  1. pip install pyserial
  2. Download zip from pyserial and save extracted library in Lib>>site-packages folder of Python.
  3. 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.

2
  • I just did this, and I still get an error that there is no module named serial
    – indymx
    Jun 8, 2020 at 20:26
  • @indymx Can you please confirm whether the pip (you used for installing pyserial) is for python x version (which you are using to compile your code)
    – Johnny
    Jul 2, 2020 at 7:05
7

In my case the command below did the job

pip3 install pyserial
5

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 
5
sudo apt install python-serial python3-serial

Solved it, using it for esp32

3

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!

2
  1. Firstly uninstall pyserial using the command pip uninstall pyserial
  2. Then go to https://www.lfd.uci.edu/~gohlke/pythonlibs/
  3. download the suitable pyserial version and then go to the directory where the file is downloaded and open cmd there
  4. then type pip install "filename"(without quotes)
2

Happened to me. Something was broken. All the solutions presented didn't work.

sudo pip install serial Requirement already satisfied: serial in /usr/local/lib/python3.8/dist-packages (0.0.97)

sudo pip3 install serial Requirement already satisfied: serial in /usr/local/lib/python3.8/dist-packages (0.0.97)

Same for pyserial, it was already installed.

Solution: I replaced the symbolic link /usr/bin/python to use python3 instead of python2.

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.