Search code examples
pythoncertificatemime

Can't Install Certificates in MacOS


I am writing a simple service with python that sends a email using MIME. and it run the code and gives me this error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 360, in send
    self.sock.sendall(s)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kiansahafi/PycharmProjects/test/Email/main_email.py", line 10, in <module>
    DatabaseRepositoryInstance.update_database_task()
  File "/Users/kiansahafi/PycharmProjects/test/Email/DatabaseRepository.py", line 22, in update_database_task
    sendEmailInstance.send_email(subject, body, to_email)
  File "/Users/kiansahafi/PycharmProjects/test/Email/send_email.py", line 63, in send_email
    server.quit()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 999, in quit
    res = self.docmd("quit")
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 426, in docmd
    self.putcmd(cmd, args)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 373, in putcmd
    self.send(str)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 363, in send
    raise SMTPServerDisconnected('Server not connected')
smtplib.SMTPServerDisconnected: Server not connected
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)

after searching i found this article that says that i have to install the certificates in python from /Applications/Python 3.11/Install Certificates.command and when i try to open it (even with the sudo command!) it gives me this error:

❯ /Applications/Python\ 3.11/Install\ Certificates.command ; exit;
 -- pip install --upgrade certifi
Requirement already satisfied: certifi in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (2023.11.17)
 -- removing any existing file or link
 -- creating symlink to certifi certificate bundle
 -- setting permissions
Traceback (most recent call last):
  File "<stdin>", line 44, in <module>
  File "<stdin>", line 40, in main
PermissionError: [Errno 1] Operation not permitted: 'cert.pem'

Saving session...completed.

[Process completed]

i searched about this as well and found this article that says i should give the terminal (and Finder!) full disk access which I did and the problem is not solved.

Picture that shows the permissions of Finder and Terminal

thanks in advance for any solutions.

my macOS version is 14.0, and my machine is a m1 macbook pro and my code editor is pycharm.

i have tried upgrading python and pip but not sure if that would do anything...


Solution

  • The problem was from interpreter which was 3.9 after changing it to 3.11 the problem was solved!