Listing available com ports with Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Listing available serial ports is the first practical step in almost any Python program that talks to hardware. Even though the title says COM ports, the same task applies across Windows, macOS, and Linux, with different device names on each platform. In Python, the standard solution is to use pyserial, which provides a cross-platform port discovery API.
Install the Right Library
The usual package is pyserial. It includes the serial communication classes and the list_ports helpers.
Once installed, use serial.tools.list_ports rather than trying to guess device names manually.
Basic Port Listing
Here is the simplest useful example:
Typical output might look like:
- Windows:
COM3 - macOS:
/dev/cu.usbserial-1410 - Linux:
/dev/ttyUSB0
This is already more reliable than hardcoding one port name and hoping the device always appears in the same place.
Get More Than Just the Device Name
Each result object contains helpful metadata such as description and hardware id.
This is useful when multiple devices are connected and you need to identify the correct one programmatically.
For example, a USB serial adapter may expose a recognizable vendor and product identifier in hwid.
Filter Ports by Description or Hardware ID
If your application works with one known type of device, filter the discovered ports instead of asking the user to guess.
This is not perfect for every device, but it is a strong starting point for hardware-specific tools.
Sort and Present Ports Predictably
On some systems, the returned order may not be the order you want to display. Sorting makes the output stable and easier to read.
Stable presentation matters when you are building a CLI or a GUI that asks the user to choose one port.
Test the Port by Opening It
Listing a port only tells you that the operating system currently exposes it. It does not prove the port is usable or that your application has permission to open it.
Use this carefully, because opening a port can affect devices that are already in use by another application.
Platform Notes
Even though the API is cross-platform, the names differ:
- Windows uses names like
COM3 - Linux often uses
/dev/ttyS0,/dev/ttyUSB0, or/dev/ttyACM0 - macOS commonly exposes
/dev/tty.*and/dev/cu.*
On macOS, cu devices are often the better choice for initiating outgoing serial connections, while tty devices have historical call-in behavior.
Common Pitfalls
- Hardcoding one port name instead of discovering ports dynamically.
- Assuming a listed port can always be opened successfully.
- Ignoring metadata like
descriptionandhwidwhen several devices are present. - Treating COM naming as Windows-only and forgetting that the same discovery problem exists on Unix-like systems.
- Opening ports aggressively during discovery and accidentally interfering with another process.
Summary
- Use
pyserialandserial.tools.list_ports.comports()to list available serial ports. - The API works across Windows, macOS, and Linux, even though device names differ.
- Metadata such as
descriptionandhwidhelps identify the correct device. - Listing a port is different from proving that it can be opened and used.
- Dynamic discovery is much safer than hardcoding one port name in serial tools.
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.