2016-04-21 70 views
1

我從C#移動到Python,和我猜我踩着一些namesapce問題,但我不能發現問題。這是當前類這是給我的錯誤(在線41上)的Python給予「模塊」對象有沒有屬性‘message_from_string’錯誤

import imaplib 
import os 
import email 

class EmailWrapper: 

hostname = None  # herp 
username = None  # derp 
password = None  # might encrypt this if there is time 

def __init__(self, host, user, passwd): 
    self.hostname = host 
    self.username = user 
    self.password = passwd 

# Create connection and return it 
def connect(self, verbose=True): 
    if verbose: print 'Connecting to ', self.hostname 
    connection = imaplib.IMAP4_SSL(self.hostname) 
    if verbose: print 'Logging in as ', self.username 
    connection.login(self.username, self.password) 
    if verbose: print 'Selecting Inbox.' 
    connection.select('Inbox') 
    return connection 

# Grab last email in inbox and return it from connection 
def get_last_email(self, c): 
    # Get list of emails 
    result, data = c.search(None, "ALL") 

    # get last ID 
    ids = data[0] 
    idList = ids.split() 
    lastEmailID = idList[-1] 

    # Fetch email 
    result, data = c.fetch(lastEmailID, "(RFC822)") 

    # Seclect body and return it 
    rawEmail = data[0][1] 
    emailMessage = email.message_from_string(rawEmail) 
    return emailMessage 


def close_connection(self, c): 
    c.close() 
    c.logout() 

任何時候,我打電話get_last_email我得到的錯誤「AttributeError的:‘模塊’對象有沒有屬性‘message_from_string’」。任何想法,將不勝感激

感謝

+1

是否例子[這裏]當你把它放在一個交互式提示(https://docs.python.org/3.5/library/email.parser.html#email.message_from_binary_file)的工作? – Natecat

+0

是的,那裏沒有錯誤。 – OmegaNine

+2

'dir(email)'的輸出是什麼?你不會在同一個文件夾中有一個名爲'email.py'的文件,對吧? – jDo

回答

2

的問題,如@jDo指出,W因爲我仍然在項目目錄中有一個空的email.py文件。謝謝!

相關問題