ImportError:没有名为'email.mime'的模块; 电子邮件不是一个包

Sup*_*ohn 18 python pip

运行以下代码时,我不断收到错误:

ImportError: No module named 'email.mime'; email is not a package
Run Code Online (Sandbox Code Playgroud)

所以我跑:

pip install email
Run Code Online (Sandbox Code Playgroud)

并得到以下错误:

ImportError: No module named 'cStringIO'...
Command "python setup.py egg_info" failed with error code 1
Run Code Online (Sandbox Code Playgroud)

互联网告诉我运行:

pip install --upgrade pip
Run Code Online (Sandbox Code Playgroud)

要解决这个问题,我现在做了很多次.我不知道还能做些什么.

Python版本: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()
Run Code Online (Sandbox Code Playgroud)

Yue*_*eng 60

我刚才遇到了同样的问题.最后,我发现这是因为我将python文件命名为'email.py'.它在更改名称后起作用.

  • 伙计,这是我很高兴在花了几个小时慢慢发疯之前检查了 StackOverflow 的事情之一。 (7认同)
  • 29 upvotes = 29人将他们的文件命名为'email.py' (3认同)
  • 同样的事情是的:) (2认同)
  • @OneStig 和计数 (2认同)
  • 我的天啊。感谢您的回答。 (2认同)

小智 16

不要在 .py 文件名中甚至在包名中使用“电子邮件”。这将导致用户声明模块和预定义模块之间的解释器混淆


Sup*_*ohn 15

问题在于pip.我无法使用更新setuptools

easy_install --upgrade setuptools
Run Code Online (Sandbox Code Playgroud)

我也无法使用pip安装电子邮件

pip install email
Run Code Online (Sandbox Code Playgroud)

我通过使用easy_install安装电子邮件来解决问题

easy_install email
Run Code Online (Sandbox Code Playgroud)

希望有人发现这有帮助.感谢那些帮助过的人.

  • 不,这很愚蠢。`email` 是标准库的一部分。 (2认同)