简体   繁体   English

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

[英]ImportError: cannot import name 'IMAP4_SSL' from 'imaplib'

I'm trying to write a Python program that downloads all the attachments from unread emails in my Gmail account and puts them into a defined folder, but when I run it, it gives me the following error: ImportError: cannot import name 'IMAP4_SSL' from 'imaplib'我正在尝试编写一个 Python 程序,该程序从我的 Gmail 帐户中的未读电子邮件中下载所有附件并将它们放入定义的文件夹中,但是当我运行它时,它会出现以下错误: ImportError: cannot import name 'IMAP4_SSL' from 'imaplib'

I read somewhere that installing the python openssl module would help, but already checked and I had it.我在某处读到安装 python openssl 模块会有所帮助,但已经检查过并且我拥有它。 I'm quite new to programming, so I'm kinda clueless here.我对编程很陌生,所以我在这里有点无能为力。 Any suggestion would be greatly appreciated!任何建议将不胜感激!

The code is as follows:代码如下:

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()

Don't know if it helps, but all the error lines that result in the terminal are:不知道它是否有帮助,但导致终端的所有错误行是:

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", line 1, in <module>
    from imaplib import IMAP4, IMAP4_SSL
ImportError: cannot import name 'IMAP4_SSL' from 'imaplib' (C:\Users\artur\anaconda3\lib\imaplib.py)

Thank you for your time!感谢您的时间!

For me, updating openssl seemed to work.对我来说,更新openssl似乎有效。

It seems like you are using conda, so看起来你正在使用 conda,所以

conda update openssl

in the Anaconda prompt.在 Anaconda 提示中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM