python无法导入smtplib

时间:2016-11-12 03:59:00

标签: python smtplib

我有一个脚本如下

#!/usr/bin/python
import smtplib
import datetime
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

now = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())

fromaddr = "xxx@gmail.com"
toaddr = "yyy@live.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Device Start Up Notification"

body = "Your device is started up %s " % now
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "abc12345")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

当我执行此文件时,它显示错误

Traceback (most recent call last):
  File "./email.py", line 2, in <module>
    import smtplib
  File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
    import email.utils
  File "/home/jypang/email.py", line 4, in <module>
    from email.MIMEMultipart import MIMEMultipart
ImportError: No module named MIMEMultipart

安装了库,因为我有另一个脚本,与上面的脚本相同(导入相同的库)并且运行良好。

请帮助,谢谢!

1 个答案:

答案 0 :(得分:4)

您将模块email命名为与标准库模块email冲突; stmplib模块取决于标准库模块email

您需要使用其他名称重命名email。如果有email.pyc,请务必将其删除。