Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pip

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 protected]'
to = ['[email protected]']
fromAddr = '[email protected]'
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()
like image 831
Super_John Avatar asked Oct 24 '15 02:10

Super_John


4 Answers

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.

like image 57
Yue Deng Avatar answered Oct 21 '22 09:10

Yue Deng


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.

like image 43
Super_John Avatar answered Oct 21 '22 11:10

Super_John


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

like image 21
Vivek Avatar answered Oct 21 '22 10:10

Vivek


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
like image 2
Simone Avatar answered Oct 21 '22 11:10

Simone