2015-10-24 16 views
8

Khi chạy đoạn code dưới đây, tôi vẫn gặp lỗi:ImportError: Không có mô-đun có tên 'email.mime'; email không phải là một gói phần mềm

ImportError: No module named 'email.mime'; email is not a package 

Vì vậy, tôi chạy:

pip install email 

Và nhận được lỗi sau:

ImportError: No module named 'cStringIO'... 
Command "python setup.py egg_info" failed with error code 1 

Internet đã yêu cầu tôi chạy:

pip install --upgrade pip 

Để giải quyết vấn đề này, điều mà tôi đã thực hiện nhiều lần. Tôi không biết tôi có thể làm gì khác.

Phiên bản Python: Python 3.3.5 | Anaconda 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() 
+8

Bạn có tệp (hoặc thư mục) được gọi là 'email.py' trong thư mục mà bạn đang chạy tập lệnh không? Hay là tập lệnh của bạn thậm chí còn được gọi là 'email.py'? – Evert

+0

@Evert nope, không có gì có tên là email trong thư mục này. –

+0

Và bạn đã thử các lần nhập khác nhau từ dấu nhắc Python chưa? '>>> nhập email',' >>> nhập email.mime'? – Evert

Trả lời

9

Vấn đề nằm trong pip. Tôi đã không thể cập nhật setuptools sử dụng

easy_install --upgrade setuptools 

Tôi cũng không thể cài đặt email với pip sử dụng

pip install email 

tôi khắc phục vấn đề bằng cách cài đặt email sử dụng easy_install

easy_install email 

Hope ai đó tìm thấy đó là hữu ích. Nhờ những người đã giúp đỡ.

16

Tôi đã gặp phải sự cố tương tự ngay lúc này. Cuối cùng, tôi tìm thấy nó là vì tôi đặt tên tệp python là 'email.py'. Nó hoạt động sau khi thay đổi tên của nó.

+0

điều tương tự có :) – JSBach

Các vấn đề liên quan