19

I'm trying to experiment with simple code to send an email from a Python script. I keep getting an error that the module 'email.MIMEMultipart' does not exist. To simplify the question/answer process, I can narrow it down even further. From the Python environment prompt I can enter

>>>import email

>>> dir(email)

It will list a bunch of modules in the email module, but none of the MIME modules are there. I can see them from WindowsExplorer in the same lib folder as all the other modules. After searching other questions I did see that I had named my test program 'email.py' which I see now is a big no-no. I deleted it and verified there isn't also an 'email.pyc'.

I'm using Windows 10, Python 3.5.2. I've also already reinstalled Python with no improvement.

Can anyone tell me what else I should check? This is the actual code:

 import smtplib
 from email.MIMEMultipart import MIMEMultipart
1
  • 1
    Beware of old instructions for Python 2, but also be aware that the email library was overhauled in Python 3.5. You can still find a lot of on-line resources which are for the pre-3.5 state of things. In particular, look for the policy argument which is new in 3.5.
    – tripleee
    Feb 6, 2020 at 6:39

1 Answer 1

63
from email.MIMEMultipart import MIMEMultipart

Is the correct import for Python 2.x.

According to the Python 3 examples in the documentation (email), you need:

from email.mime.multipart import MIMEMultipart
2
  • 4
    I was the one tagging it Python 3 and felt that it doesn’t hurt to know that the code OP possibly found somewhere looks like it does, because it is meant for Python 2. It didn’t mean to be smug, no bad feelings here.
    – x squared
    Nov 2, 2017 at 9:40
  • 1
    If you don’t like it, I can put it in a comment instead.
    – x squared
    Nov 2, 2017 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.