导入错误:无法从“imaplib”导入名称“IMAP4_SSL”

问题描述

我正在尝试编写一个 Python 程序,该程序从我的 Gmail 帐户中的未读电子邮件中下载所有附件并将它们放入一个定义的文件夹中,但是当我运行它时,它给了我以下错误:ImportError: cannot import name 'IMAP4_SSL' from 'imaplib'

我在某处读到安装 python openssl 模块会有所帮助,但已经检查过了,我已经有了。我对编程很陌生,所以我在这里有点无能为力。任何建议将不胜感激!

代码如下:

import os
from imbox import Imbox
import traceback


host = "imap.gmail.com"
username = 'account'
password = 'password'
download_folder = 'C:\\Users\\artur\\Desktop\\test'

if not os.path.isdir(download_folder):
    os.makedirs(download_folder,exist_ok=True)
    
mail = Imbox(host,username=username,password=password,ssl=True,ssl_context=None,starttls=False)
messages = mail.messages(unread=True) # defaults to inbox

for (uid,message) in messages:
    mail.mark_seen(uid) # optional,mark message as read

    for idx,attachment in enumerate(message.attachments):
        try:
            att_fn = attachment.get('filename')
            download_path = f"{download_folder}/{att_fn}"
            print(download_path)
            with open(download_path,"wb") as fp:
                fp.write(attachment.get('content').read())
        except:
            pass
            print(traceback.print_exc())

mail.logout()

不知道有没有用,但是导致终端的所有错误行是:

File "c:/path/Gmail_downloader.py",line 2,in <module>
    from imbox import Imbox
  File "C:\Users\artur\anaconda3\lib\site-packages\imbox\__init__.py",line 1,in <module>
    from imbox.imbox import Imbox
  File "C:\Users\artur\anaconda3\lib\site-packages\imbox\imbox.py",line 3,in <module>
    from imbox.imap import ImapTransport
  File "C:\Users\artur\anaconda3\lib\site-packages\imbox\imap.py",in <module>
    from imaplib import IMAP4,IMAP4_SSL
ImportError: cannot import name 'IMAP4_SSL' from 'imaplib' (C:\Users\artur\anaconda3\lib\imaplib.py)

感谢您的宝贵时间!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...