ESP32 IDE Compiler 'ImportError: No module named serial'

Howdy,

I am in the process of making a portable Arduino IDE system to allow access & upload of ino sketches to microcontrollers already installed in buildings on my property. I have a Raspberry Pi I am using for this setup. I have used it successfully to upload sketches to an Arduino Uno. The IDE being used on the PI is version 1.8.19.

My issue is with an ESP32 WROOM microcontroller. The compiler is generating an error regarding a module that is referenced called "serial." The ESP32 library package is installed in the IDE. This error does not occur on my other computer using a Linux based IDE platform.
I ran this command script hoping it would straighten things up:

sudo usermod -a -G dialout $USER && \
   sudo apt-get install git && \
   wget https://bootstrap.pypa.io/get-pip.py && \
   sudo python3 get-pip.py && \
   sudo pip3 install pyserial && \
   mkdir -p ~/Arduino/hardware/espressif && \
   cd ~/Arduino/hardware/espressif && \
   git clone https://github.com/espressif/arduino-esp32.git esp32 && \
   cd esp32/tools && \
   python3 get.py

Where should I look to correct this missing module file?
thanks for any ideas.....
Compiler error message:

Traceback (most recent call last):
  File "/home/pi/.arduino15/packages/esp32/tools/esptool_py/3.1.0/esptool.py", line 38, in <module>
    import serial
ImportError: No module named serial
Multiple libraries were found for "WiFi.h"
 Used: /home/pi/.arduino15/packages/esp32/hardware/esp32/2.0.2/libraries/WiFi
 Not used: /home/pi/Arduino/libraries/WiFiNINA
 Not used: /opt/arduino-1.8.19/libraries/WiFi
exit status 1
Error compiling for board ESP32 Dev Module.

Try this:

sudo pip install pyserial

It is a bit of a mess regarding whether to use python or python3 and pip or pip3 on Linux.

Hmmm...it seems to already think it is setup:

pi@raspberrypi:~ $ sudo pip install pyserial
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pyserial in /usr/lib/python3/dist-packages (3.5b0)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

edit -
fyi:

pi@raspberrypi:~ $ pip3 show pyserial
Name: pyserial
Version: 3.5b0
Summary: Python Serial Port Extension
Home-page: https://github.com/pyserial/pyserial
Author: Chris Liechti
Author-email: cliechti@gmx.net
License: BSD
Location: /usr/lib/python3/dist-packages
Requires: 
Required-by: mote

Several years ago when I tried to develop and upload code to an ESP32 from a RPI, I found out that ESPRESSIF no longer developed a core for the RPI. This may still be the case. You might inquire at ESP32 Forum - Index page.

There is an old ESP32 core that does work with the RPi. The core has to be complied on the RPi. For me, after 24 hours and it was still working on the compile of the core, I gave up. At the time ESPRESSIF's response was to use a PC.

Thanks for that info. Not what I was hoping to hear though... lol.

Try this one:

python -m pip install pyserial

terminal result:

/usr/bin/python: No module named pip

Try doing this to get the pip module:

Okay, here is terminal output on pip inquiry:

pi@raspberrypi:~ $ python -m ensurepip
ensurepip is disabled in Debian/Ubuntu for the system python.

Python modules For the system python are usually handled by dpkg and apt-get.

    apt-get install python-<module name>

Install the python-pip package to use pip itself.  Using pip together
with the system python might have unexpected results for any system installed
module, so use it on your own risk, or make sure to only use it in virtual
environments.

pi@raspberrypi:~ $ apt-get install python-pip
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
pi@raspberrypi:~ $ sudo apt-get install python-pip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package python-pip is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  python3-pip

E: Package 'python-pip' has no installation candidate
pi@raspberrypi:~ $ 

Update: I discovered that the file:

/home/pi/.arduino15/packages/esp32/tools/esptool_py/3.1.0/esptool.py
shows version 3. 1 for python while terminal commands showed version 2.7.18. Is this discrepancy causing the compiler 'serial' module not found issue? What is correct path forward? I noticed the esptool.py file suggested:
"You may be able to work around this by 'pip uninstall serial; pip install pyserial'
but this may break other installed Python software that depends on 'serial'."

pi@raspberrypi:~ $ #!/usr/bin/env python
pi@raspberrypi:~ $ which python
/usr/bin/python
pi@raspberrypi:~ $ python --version
Python 2.7.18
pi@raspberrypi:~ $ 

top section of shows version 3.1 of esptool.py [after initial comment area]:

from __future__ import division, print_function

import argparse
import base64
import binascii
import copy
import hashlib
import inspect
import io
import itertools
import os
import shlex
import string
import struct
import sys
import time
import zlib

try:
    import serial
except ImportError:
    print("Pyserial is not installed for %s. Check the README for installation instructions." % (sys.executable))
    raise

# check 'serial' is 'pyserial' and not 'serial' https://github.com/espressif/esptool/issues/269
try:
    if "serialization" in serial.__doc__ and "deserialization" in serial.__doc__:
        raise ImportError("""
esptool.py depends on pyserial, but there is a conflict with a currently installed package named 'serial'.

You may be able to work around this by 'pip uninstall serial; pip install pyserial' \
but this may break other installed Python software that depends on 'serial'.

There is no good fix for this right now, apart from configuring virtualenvs. \
See https://github.com/espressif/esptool/issues/269#issuecomment-385298196 for discussion of the underlying issue(s).""")
except TypeError:
    pass  # __doc__ returns None for pyserial

try:
    import serial.tools.list_ports as list_ports
except ImportError:
    print("The installed version (%s) of pyserial appears to be too old for esptool.py (Python interpreter %s). "
          "Check the README for installation instructions." % (sys.VERSION, sys.executable))
    raise
except Exception:
    if sys.platform == "darwin":
        # swallow the exception, this is a known issue in pyserial+macOS Big Sur preview ref https://github.com/espressif/esptool/issues/540
        list_ports = None
    else:
        raise


__version__ = "3.1"

MAX_UINT32 = 0xffffffff
MAX_UINT24 = 0xffffff

Try
pip3 install pyserial
as a regular user

Still getting the ill-fated "ImportError: no module named serial". :cry:

terminal output:

pi@raspberrypi:~ $ pip3 install pyserial
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pyserial in /usr/lib/python3/dist-packages (3.5b0)
pi@raspberrypi:~ $ [quote="ZX80, post:11, topic:968605, full:true"]

I had the same problem on Ubuntu 20.
The solution was (part of ?)
sudo apt install python-is-python3
I think it is available in Debian too.
https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3/1031733#1031733

Using this link:
Python Programming Tutorial: Getting Started with the Raspberry Pi - SparkFun Learn.

I went in terminal and appended .bashrc script file:

alias python='/usr/bin/python3'

and

alias pip=pip3

When I now do a "python --version" check it returns:
Python 3.9.2

When before it was returning a version 2 value.
But still I get the same compiler error: ImportError: no module named serial

What next???????

The file acts as a shell script that is run each time that specific user opens a terminal (or logs in over SSH, Serial, etc.).

So I don't think it is a very useful solution.
Good luck

sudo apt install python-serial

should install pyserial for Python 2.7.

Hi guys,
don't remember where i've read this but for me running

sudo apt instal python-is-python3 worked.

Now the IDE compiles for ESP32 boards.

2 Likes

daarig,

Holy Crap!!!! Your suggestion was a winner!!!!
I hope you get a visit from the Publishers Clearinghouse Winner Announcement Team and they hand you a big fat check!!!!! :star_struck:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.