user8548: 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)


user1677: There can be several reasons as highlighted in these questions:

https://stackoverflow.com/questions/6032568/using-serial-on-python-win7
https://stackoverflow.com/questions/6105182/error-while-opening-port-in-python

They include:

port is already in use by another application
permissions are set to deny access to normal users
problems in the code (top answer in first link)

Try to run with administrator priviledges ("run as administrator"). Others claim that a simple retry might help.

user45188: Either run your python program in "administrator mode" or close any other programs using the port intended for

user58552: Are you by any chance using Jupyter Notebok? I was having a similar error today and managed to solve it by opening Jupyter Notebook through Anaconda Navigator instead of opening it through Anaconda Prompt. 

user70011: you must use this code
>>> import serial
>>> ser.close()             # close port


user-1: 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.

8548: <p>I am new to programming and am trying to communicate with my arduino using python through serial communication. I am using the following code:</p>

<p>Arduino code:</p>

<pre><code>int ledPin = 11;

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

void loop() {
  digitalWrite(ledPin,LOW);
    if (Serial.read() == 'M'){
      digitalWrite(ledPin,HIGH);}
}
</code></pre>

<p>Python code:</p>

<pre><code>import imaplib
import serial
ser = serial.Serial("COM3",9600)
ser.write(b'T')
ser.close()
</code></pre>

<p>I get the following error on running the python script:</p>

<blockquote>
  <p>serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)</p>
</blockquote>
 1677: <p>There can be several reasons as highlighted in these questions:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/6032568/using-serial-on-python-win7">https://stackoverflow.com/questions/6032568/using-serial-on-python-win7</a></li>
<li><a href="https://stackoverflow.com/questions/6105182/error-while-opening-port-in-python">https://stackoverflow.com/questions/6105182/error-while-opening-port-in-python</a></li>
</ul>

<p>They include:</p>

<ul>
<li>port is already in use by another application</li>
<li>permissions are set to deny access to normal users</li>
<li>problems in the code (top answer in first link)</li>
</ul>

<p>Try to run with administrator priviledges ("run as administrator"). Others claim that a simple retry might help.</p>
 45188: <p>Either run your python program in "administrator mode" or close any other programs using the port intended for</p>
 58552: <p>Are you by any chance using Jupyter Notebok? I was having a similar error today and managed to solve it by opening Jupyter Notebook through Anaconda Navigator instead of opening it through Anaconda Prompt. </p>
 70011: <p>you must use this code</p>
<pre><code>&gt;&gt;&gt; import serial
&gt;&gt;&gt; ser.close()             # close port
</code></pre>
 -1: <p>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.</p>
