Search code examples
python-3.xpyserial

"install serial" vs "install pyserial" headbut


the problem

I've been handed a python script that requires (among other things):

import serial
import serial.tools.list_ports

(I'm using pipenv, but assume this is the same as pip.)

When I install pyserial, running the script gives me repeated errors:

get_com_port_frame: 'Serial' object has no attribute 'set_buffer_size'
(Re)starting frames com port: /dev/cu.usbmodem21401

When I instead install serial, I get:

>>> import serial
>>> import serial.tools.list_ports
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'serial.tools'

the environment:

Running macOS Sonoma 14.4 on M1 silicon with Python 3.12.3

the question:

What am I missing? What's the fix?


Solution

  • Hard to say what your problem is without access to the original source of get_com_port_frame, and the context in which this was developed and executed, but based on the imports it seems like you should be installing pyserial which has the serial.tools.list_ports module you're trying to import. The serial package seems to be unrelated, and just happens to share the same namespace.

    In the source code, the set_buffer_size function seems to have been there for 9 years, so I doubt your problem is a version issue unless you're using a prehistoric version of this class.

    My hunch would be that you're somehow not using the intended Serial class here -- the function imported is in a win32 version of the class, under a comment that says - platform specific -. It's also not in the documentation for the serial.Serial class.

    Could it be that the script you inherited worked on Windows, but you're porting to a different platform where some assumptions don't hold anymore? You do mention in the question running on Mac, so that seems likely. To confirm, I'd inspect the object at the point where you get the attribute error to see what class it really is, because the source code for pyserial has many classes names Serial.