54

When running the below code, I keep getting the error:

ImportError: No module named 'email.mime'; email is not a package

So I run:

pip install email

And get the following error:

ImportError: No module named 'cStringIO'...
Command "python setup.py egg_info" failed with error code 1

The internet has told me to run:

pip install --upgrade pip

To solve this problem, which I've done many times now. I don't know what else I can do.

Python version: Python 3.3.5 | Anaconda 2.3.0 (x86_64)

import smtplib,email,email.encoders,email.mime.text,email.mime.base

smtpserver = 'email@site.com'
to = ['address@gmail.com']
fromAddr = 'email@site.com'
subject = "testing email attachments"

# create html email
html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
html +='"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'
html +='<body style="font-size:12px;font-family:Verdana"><p>...</p>'
html += "</body></html>"
emailMsg = email.MIMEMultipart.MIMEMultipart('text/csv')
emailMsg['Subject'] = subject
emailMsg['From'] = fromAddr
emailMsg['To'] = ', '.join(to)
emailMsg['Cc'] = ", ".join(cc)
emailMsg.attach(email.mime.text.MIMEText(html,'html'))

# now attach the file
fileMsg = email.mime.base.MIMEBase('text/csv')
fileMsg.set_payload(file('rsvps.csv').read())
email.encoders.encode_base64(fileMsg)
fileMsg.add_header('Content-Disposition','attachment;filename=rsvps.csv')
emailMsg.attach(fileMsg)

# send email
server = smtplib.SMTP(smtpserver)
server.sendmail(fromAddr,to,emailMsg.as_string())
server.quit()
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Super_John
  • 1,767
  • 2
  • 14
  • 27
  • 21
    Do you have a file (or directory) called `email.py` in the directory where you are running the script? Or is your script even called `email.py`? –  Oct 24 '15 at 02:25
  • @Evert nope, nothing named email in this directory. – Super_John Oct 24 '15 at 02:28
  • And have you tried the various imports from the Python prompt? `>>> import email`, `>>> import email.mime`? –  Oct 24 '15 at 02:29
  • Yes, I have. - Traceback (most recent call last): File "", line 1517, in _find_and_load_unlocked AttributeError: 'module' object has no attribute '__path__' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "./email.py", line 4, in import smtplib File "/Users/john.houghton/anaconda/envs/py3k/lib/python3.3/smtplib.py", line 47, in import email.utils ImportError: No module named 'email.utils'; email is not a package – Super_John Oct 24 '15 at 02:32
  • 3
    "File "", line 1, in File "./email.py", line 4": that looks suspiciously like your script is actually called `email.py`. –  Oct 24 '15 at 02:34
  • @Evert This script is not, however, I did find one in this directory with the name 'email.py', which was a dumb mistake. Now having changed the name of that file, I'm still seeing the same error. – Super_John Oct 24 '15 at 03:14
  • 1
    There's likely a email.pyc file or \_\_pycache\_\_ directory still present which contains the compiled version of that file. –  Oct 24 '15 at 03:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93223/discussion-between-super-john-and-evert). – Super_John Oct 24 '15 at 03:36
  • @Evert Heh, brilliant. That was my problem... – Soren Bjornstad Apr 19 '17 at 13:18

4 Answers4

202

I encountered the same problem just now. Finally, I found it's because I name the python file as 'email.py'. It works after changing its name.

Spencer
  • 2,276
  • 3
  • 10
  • 15
32

Don't use "email" in your .py file name or even in package name as well. This will cause confusion to the interpreter between user declared module and pre-defined modules

Vivek
  • 566
  • 5
  • 6
  • 1
    This is one of the most stupid gotachs! Why do we have all the namespaces, packages, modules etc if python isn't able to distinguish between them? :-\ – t3chb0t Apr 03 '23 at 07:58
  • 1
    Strangly the conflict only occurs when an email package is placed directly under src. It goes away when moved into a subdirectory. – t3chb0t Apr 03 '23 at 09:03
21

The issue is in pip. I was unable to update setuptools using

easy_install --upgrade setuptools

I was also unable to install email with pip using

pip install email

I fixed the problem by installing email using easy_install

easy_install email

Hope someone finds that as helpful. Thanks to those who have helped.

Super_John
  • 1,767
  • 2
  • 14
  • 27
2

I had the same problem. Both answers helped me solve it. However, in addition I also had to delete the email.pyc file that was created when the script was run with the old name email.py.

To summarize:

  • make sure the email module is installed
  • delete the email.pyc file that was created when the script called email.py was run
  • rename the script to something else
Simone
  • 21
  • 1