2016-11-12 91 views
1

我有一個腳本如下蟒蛇未能導入的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 = "[email protected]" 
toaddr = "[email protected]" 
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 

庫被安裝,因爲我有另一個腳本相同上面的腳本(導入相同的庫),它運行良好。

請協助,謝謝!

回答

4

您將您的模塊email命名爲與標準庫模塊email衝突; stmplib模塊取決於標準庫模塊email

您需要用其他名稱重命名email。如果有email.pyc,請確保將其刪除。

+1

感謝您抽出寶貴的教訓!也必須刪除email.pyc –