How to fix python-can can.exceptions.CanInterfaceNotImplementedError: The serial module is not installed

Problem

You’re using the python-can library and get the following error:

You won't be able to use the slcan can backend without the serial module installed!
Traceback (most recent call last):
  File "/home/uli/TestCAN.py", line 10, in <module>
    bus = can.interface.Bus(bustype='slcan', channel=slcan_device, bitrate=baud_rate)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/uli/.pyenv/versions/3.11.9/lib/python3.11/site-packages/can/util.py", line 379, in wrapper
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/uli/.pyenv/versions/3.11.9/lib/python3.11/site-packages/can/interface.py", line 135, in Bus
    bus = cls(channel, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/uli/.pyenv/versions/3.11.9/lib/python3.11/site-packages/can/interfaces/slcan.py", line 98, in __init__
    raise CanInterfaceNotImplementedError("The serial module is not installed")
can.exceptions.CanInterfaceNotImplementedError: The serial module is not installed

Solution

The error message is pretty clear: You need to install the serial module. You can do this via pip:

pip install pyserial