2015-10-24 2 views
8

Quando si esegue il codice qui sotto, continuo a ricevere l'errore:ImportError: nessun modulo chiamato 'email.mime'; e-mail non è un pacchetto

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

Allora corro:

pip install email 

E ottenere il seguente errore:

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

Internet mi ha detto di eseguire:

pip install --upgrade pip 

Per risolvere questo problema, cosa che ho fatto molte volte adesso. Non so cos'altro posso fare.

Versione 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

Hai un file (o una directory) chiamato 'email.py' nella directory in cui stai eseguendo lo script? O il tuo script ha anche chiamato 'email.py'? – Evert

+0

@Evert nope, nessuna e-mail con nome in questa directory. –

+0

E hai provato le varie importazioni dal prompt di Python? '>>> importa email',' >>> importa email.mime'? – Evert

risposta

9

Il problema è in pip. Ero in grado di aggiornare setuptools usando

easy_install --upgrade setuptools 

Sono stato anche in grado di installare e-mail con pip usando

pip install email 

ho risolto il problema con l'installazione di e-mail utilizzando easy_install

easy_install email 

Speranza qualcuno trova quello come utile. Grazie a coloro che hanno aiutato.

16

Ho riscontrato lo stesso problema proprio ora. Infine, ho scoperto che è il nome del file python come "email.py". Funziona dopo aver cambiato il suo nome.

+0

stessa cosa sì :) – JSBach

Problemi correlati