Python给出“模块”对象没有属性'message_from_string'错误

时间:2016-04-21 01:49:58

标签: python email

我正在从C#转向Python,我猜我正在踩着一些名字问题,但我找不到问题。这是给我错误的当前类(第41行)

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:'module'对象没有属性'message_from_string'”。任何想法都将不胜感激。

由于

1 个答案:

答案 0 :(得分:5)

正如@jDo所指出的那样,问题是我在项目目录中仍然有一个空的email.py文件。谢谢!