Communicating via COM port

So, I am trying to send a character from my python program to Arduino program via the COM5 serial port. However, It wont let me run both at the same time because it says that the port is busy.

This is my python code:

import serial
import time
try:
    arduino = serial.Serial()
    arduino.port = 'COM5'
    time.sleep(2)
    
except Exception as e:
    print(e)

arduino.open()
arduino.write(b'a')
time.sleep(2)
arduino.close()

This is my Arduino code which is the exact same of Robin2's serial basics code.

char receivedChar;
boolean newData = false;

void setup() {
 Serial.begin(9600);
 Serial.println("<Arduino is ready>");
}

void loop() {
 recvOneChar();
 showNewData();
}

void recvOneChar() {
 if (Serial.available() > 0) {
 receivedChar = Serial.read();
 newData = true;
 }
}

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChar);
 newData = false;
 }
}

Can anyone tell me what I am doing wrong? Thanks.

You can have only one thing connected to a serial port at a time. The solution is to use one port for the serial monitor and another for the Python port. You do that by connecting a USB to TTL converter (FTDI) to a different serial port and to the PC Python port. Some Arduinos have extra hardware serial ports (Mega) or you can use a software serial library to create a port (Uno).

rt1122:
However, It wont let me run both at the same time because it says that the port is busy.

What do you mean by "both"?

if you mean the Python program and the Arduino Serial Monitor then close the Serial Monitor before you run your Python program. And close the Python program before you try to upload new code or use the Serial Monitor.

...R