Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

Python says module smtplib has no attribute SMTP

ErrorAttributeError: 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.

  • Applies to Python 3.12, Python 3.13
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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__)"
  2. 2Match what you see, then jump to its fix

    1. 1The printed path is in your project folder, next to the scriptYour own file is named smtplib.pyFix
    2. 2The script folder is clean but the printed path points into a folder you added to PYTHONPATHA folder on PYTHONPATH holds a file with that nameFix

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/null

2Fix

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

Did this fix it?

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 PYTHONPATH

3Verify

Import again and print the path and the class.

python -c "import smtplib; print(smtplib.__file__); print(smtplib.SMTP)"

Confirmed by docs.python.org

Did this fix it?

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:

Related fixes

Email

Django 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.