gpt4 book ai didi

Python给出 "module'对象没有属性 'message_from_string'错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:04 25 4
gpt4 key购买 nike

我正在从 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’”。任何想法将不胜感激。

谢谢

最佳答案

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

关于Python给出 "module'对象没有属性 'message_from_string'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36758193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com