Search code examples
pyserial

pySerial running command to list ports


I am using pySerial and I am running this command using CMD to list available COM ports and displays a COM port number when found:

python -m serial.tools.list_ports

I know that the command line will import the serial module when I use the python -m flag and I can access the objects inside it so it should show the output. However, the same command however does not work when run using the IDLE shell:

import serial
print(serial.tools.list_ports_common)

This returns an error AttributeError: module 'serial' has no attribute 'tools' Why is it not working at IDLE?


Solution

  • You need to import it first:

    from serial.tools import list_ports
    list_ports.main()  # Same result as python -m serial.tools.list_ports
    

    You can check out the source here