Python says module smtplib has no attribute SMTP
AttributeError: module 'smtplib' has no attribute 'SMTP'Short answer
Python found a different smtplib than the standard one. The script's own folder comes first on the import path, so a file named smtplib.py there wins. A folder listed in PYTHONPATH can do the same. Print smtplib.__file__ to see which file loaded, then rename it.
Fix it fast
1Find out which cause you have
Ask Python which file it imported as smtplib. A path in the standard library folder is the real module; any other path is the culprit.
python -c "import smtplib; print(smtplib.__file__)"2Match what you see, then jump to its fix
What does the error look like?
AttributeError: module 'smtplib' has no attribute 'SMTP'print(smtplib.__file__) shows a path inside your own project
Cause 1 of 2
Your own file is named smtplib.py
1Check
The Python tutorial says the directory with the input script is first in sys.path, so scripts there are loaded instead of library modules of the same name.
ls smtplib.py* __pycache__ 2>/dev/null2Fix
Rename the file to something else, such as send_mail.py, and delete the cached copy.
mv smtplib.py send_mail.py
rm -rf __pycache__3Verify
Run the one-liner again. The path should now point into the standard library.
python -c "import smtplib; print(smtplib.__file__); print(smtplib.SMTP)"Confirmed by docs.python.org
Cause 2 of 2
A folder on PYTHONPATH holds a file with that name
1Check
PYTHONPATH comes next in sys.path, ahead of the installation defaults. List the folders and search them.
echo "$PYTHONPATH"
python -c "import sys; print(sys.path)"2Fix
Rename the file in that folder, or remove the folder from PYTHONPATH for this project.
unset PYTHONPATH3Verify
Import again and print the path and the class.
python -c "import smtplib; print(smtplib.__file__); print(smtplib.SMTP)"Confirmed by docs.python.org
How do I stop it coming back?
- Never name a project file after a standard library module (smtplib, email, random, json and so on).
- Name scripts by what they do, for example send_report.py.
- Keep one virtual environment per project so a stray PYTHONPATH does not leak between them.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Python smtplib raises SMTPServerDisconnected: please run connect() first
smtplib.SMTPServerDisconnected: please run connect() first
smtplib raises SMTPServerDisconnected when you use an SMTP object that is not connected.
3 causes, each with a fixDjango send_mail fails or hangs with an SSL error
smtplib.SMTPException raised from send_mail()
Django has two secure modes that must not be mixed: EMAIL_USE_TLS for explicit TLS (STARTTLS, usually port 587) and EMAIL_USE_SSL for implicit TLS (usually port 465).
4 causes, each with a fixGmail SMTP blocks you with 454 4.7.0 Too many login attempts
SMTP Error: 454 4.7.0 Too many login attempts
454 4.7.0 is a temporary block.
2 causes, each with a fixGmail rejects your mail with 530 5.5.1 Authentication Required
530 5.5.1 Authentication Required
Gmail refuses to accept a message from a connection that has not signed in.
3 causes, each with a fixGmail SMTP login fails with 535 or 534 5.7.14
534-5.7.14 Please log in via your web browser and then try again.
Gmail turned your sign-in down.
3 causes, each with a fixPython IMAP OAuth login to a shared mailbox in Microsoft 365 fails
imaplib.IMAP4.error: AUTHENTICATE failed.
Exchange Online accepts an OAuth token for IMAP only if the SASL user name is right and the token's identity has access to that mailbox.
4 causes, each with a fix