[Python] how to resolve' NameError: name 'imaplib' is not defined' (python poetry)

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I spend a lot of time resolving this problem but I couldn't, So I want someone to help me.

    I'm beginner for python and use poetry.

    When I import imaplib I always get an error. ' NameError: name 'imaplib' is not defined'

    I know I don't need to install imaplib'. what should I do then?

    This below is my code.

    Thank you for your reading and kindness. I appreciate it.


    import imaplib
    import email

    host = 'imap.gmail.com'
    username = 'xxxxxxx@gmail.com'
    password = 'xxxxxxxx'

    mail = imaplib.IMAP4_SSL(host)
    mail.login(username, password)
    mail.select('inbox')
    _, search_data = mail.search(None, 'UNSEEN')

    for num in search_data[0].split():
    # print(num)
    _, data = mail.fetch(num, '(RFC822)')
    # print(data[0])
    _, b = data[0]
    email_message = email.message_from_bytes(b)
    #email_message = email.message_from_string(b)

    # print(email_message)
    for part in email_message.walk():
    if part.get_content_type() == 'text/plain' or part.get_content_type() == 'text/html':
    body = part.get_payload(decode=True)
    print(body)
    print(search_data)

    Continue reading...

Compartilhe esta Página