【发布时间】:2016-01-23 16:04:44
【问题描述】:
运行以下代码时,我不断收到错误:
ImportError: No module named 'email.mime'; email is not a package
所以我跑了:
pip install email
并得到以下错误:
ImportError: No module named 'cStringIO'...
Command "python setup.py egg_info" failed with error code 1
互联网告诉我要跑步:
pip install --upgrade pip
为了解决这个问题,我已经做过很多次了。我不知道我还能做什么。
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()
【问题讨论】:
-
在您运行脚本的目录中是否有一个名为
email.py的文件(或目录)?或者你的脚本甚至被称为email.py? -
@Evert nope,此目录中没有任何命名的电子邮件。
-
您是否尝试过从 Python 提示符进行各种导入?
>>> import email,>>> import email.mime? -
"文件 "
",第 1 行,在 文件 "./email.py",第 4 行":这看起来很可疑,您的脚本实际上被称为 email.py。 -
可能仍然存在 email.pyc 文件或 __pycache__ 目录,其中包含该文件的编译版本。