简体   繁体   中英

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'

I read somewhere that installing the python openssl module would help, but already checked and I had it. 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.

It seems like you are using conda, so

conda update openssl

in the Anaconda prompt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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