Can't communicate with arduino using python (Windows) I am new to programming and am trying to communicate with my arduino using python through serial communication. I am using the following code:

Arduino code:

int ledPin = 11;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}

void loop() {
  digitalWrite(ledPin,LOW);
    if (Serial.read() == 'M'){
      digitalWrite(ledPin,HIGH);}
}


Python code:

import imaplib
import serial
ser = serial.Serial("COM3",9600)
ser.write(b'T')
ser.close()


I get the following error on running the python script:


  serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)
More than likely you ran the Arduino program from the Arduino IDE, and left the terminal window open. You must close it before you run the python program, as it will already 'own' the port until it closes. You dont have to quit Arduino IDE, just close the terminal window.