我正在使用此Python脚本来测试以编程方式发送电子邮件:
from mailer import Mailer
from mailer import Message
message = Message(From="myname@gmail.com",
To="myname@aol.com",
charset="utf-8")
message.Subject = "An HTML Email"
message.Html = """This email uses <strong>HTML</strong>!"""
message.Body = """This is alternate text."""
sender = Mailer('smtp.gmail.com')
sender.send(message)
我相信我已经安装了Mailer-0.7(但我该怎么检查?),并在这个iMac上运行Python 2.7。麻烦的是我收到此错误消息:
Traceback (most recent call last):
File "email.py", line 1, in <module>
from mailer import Mailer
File "/Users/jeremycondie/Desktop/mailer.py", line 31, in <module>
import smtplib
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 46, in <module>
import email.utils
File "/Users/jeremycondie/Desktop/email.py", line 1, in <module>
from mailer import Mailer
ImportError: cannot import name Mailer
答案 0 :(得分:3)
from mailer import X
语句正在从/Users/jeremycondie/Desktop/mailer.py
导入,请尝试重命名该文件。
编辑:获取Mailer的版本(修复文件名后):
import mailer
print mailer.__version__
编辑2: 如果将来遇到类似的情况,首先检查模块位置及其属性,以丢弃本地文件正在模块化模块:
print mailer.__file__
print dir(mailer)