Search code examples
pythonemailsmtplib

Smtplib gives partially initialized module error when trying to use it in python


I'm trying to make a python script that emails me stuff and i followed this tutorial about how to do it and i thought of making it work using SMTP_SSL()

import smtplib, ssl

port = 465
password = input("Type your password and press enter: ")

context = ssl.create_default_context()

sender_email = "me@gmail.com"
receiver_email = "some@gmail.com"
message = """\
Subject: Hi there

This message is sent from Python."""

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login(receiver_email, password)
    server.sendmail(sender_email, receiver_email, message)

But it keeps giving me this error:

ImportError: cannot import name 'SMTP' from partially initialized module 'smtplib' (most likely due to a circular import)

Can anyone help me clarify what this error means and if there's a workaround for it. Any help appreciated.


Solution

  • I ran into this error too, in my case it was because I named the script email.py.

    If anyone face this error in the future, for debugging purposes try using a different filename like my_email_script.py