could not open port (Python and Arduino)

  • Python
  • Arduino
  • ESP32

You may see

serial.serialutil.SerialException: could not open port 'COM99': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)
could not open port /dev/ttyACM0

Why it happens

  • The Serial Monitor or Arduino IDE has the port.
  • The port's name is wrong.
  • On Linux you aren't in the dialout group.

How to fix it

  1. Close the Serial Monitor.
  2. Run python -m serial.tools.list_ports to see the names.
  3. On Linux: sudo usermod -a -G dialout $USER, then log out and in.
  4. Use the same baud rate on both sides and wait 2 seconds after opening.

It worked if: Python prints the first line from the board.

Seen in a real run (some lines are close variants of it). The FileNotFoundError part was produced here by esptool, which uses pyserial; the rest is pyserial's and Windows' usual wording. Sources: pySerial documentation.

Didn't help? Search another error or ask us.