I am trying to delete some emails, but when I run the code I get the following error: TypeError: 'module' object is not callable (env)

I am trying to delete some emails, but when I run the code I get the following error: TypeError: 'module' object is not callable (env)

我试图通过 Python 删除一些电子邮件,但是当我 运行 代码(通过 git bash)时,我收到以下错误: 我试图通过 Python 删除一些电子邮件,但是当我 运行 代码(通过 git bash)时,我收到以下错误:

$ python imap_email.py
Traceback (most recent call last):
  File "imap_email.py", line 6, in <module>
    env = environ()
TypeError: 'module' object is not callable
(env)

这是我的代码

import imaplib
import email
from email.parser import HeaderParser
import environ

env = environ()

environ.Env.read_env()

server ='outlook.office365.com'
user = env('EMAIL_USER')
password = env('EMAIL_PASSWORD')

def connect(server, user, password):
    imap_conn = imaplib.IMAP4_SSL(server)
    imap_conn.login(user=user, password=password)
    return imap_conn

def delete_email(instance, email_id) :
    typ, delete_response = instance.fetch(email_id, '(FLAGS)')
    typ, response = instance.store(email_id, '+FLAGS', r'(\Deleted)')
    print(delete_response)
    print(response)
    
keywords_list = ['You won', 'Bitcoin']

for keyword in keywords_list:
    conn = connect(server, user, password)
    conn.select( "Spam")
    typ, msg = conn.search(None, '(BODY"' + keyword + '")')
    print(msg)
    msg = msg[0].split()
    for email_id in msg:
        print(email_id)
        delete_email(conn, email_id)
        
    print('Complete !')

提前致谢

参见 documentation

中的示例
env = environ.Env()

而你忘记了 .Env

就这些了。