Python 导入错误:没有名为“email.mime”的模块;电子邮件不是包裹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33313858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named 'email.mime'; email is not a package
提问by Super_John
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)
Python 版本:Python 3.3.5 | 蟒蛇 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()
采纳答案by Super_John
The issue is in pip. I was unable to update setuptools using
问题出在点子上。我无法使用更新 setuptools
easy_install --upgrade setuptools
I was also unable to install email with pip using
我也无法使用 pip 安装电子邮件
pip install email
I fixed the problem by installing email using easy_install
我通过使用 easy_install 安装电子邮件解决了这个问题
easy_install email
Hope someone finds that as helpful. Thanks to those who have helped.
希望有人觉得这很有帮助。感谢那些帮助过的人。
回答by Yue Deng
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.
我刚才遇到了同样的问题。最后,我发现这是因为我将 python 文件命名为“email.py”。更改名称后即可使用。
回答by Simone
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.
我有同样的问题。两个答案都帮助我解决了它。但是,此外,我还必须删除使用旧名称 email.py 运行脚本时创建的 email.pyc 文件。
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
- 确保已安装电子邮件模块
- 删除名为 email.py 的脚本运行时创建的 email.pyc 文件
- 将脚本重命名为其他名称
回答by Mayuresh Satao
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
不要在 .py 文件名中甚至在包名中使用“电子邮件”。这将导致用户声明模块和预定义模块之间的解释器混淆