Search code examples
pythonpython-3.ximaplib

Cannot resolve module 'imaplib2' has no attribute 'IMAP4_SSL'


There are many questions regarding imaplib2 has no attribute IMAP4_SSL but none of the solutions in the other questions worked for me.

I have a python script that's been running without issue for years on AWS, but after I upgraded to Ubuntu 20.04.2 LTS, it started throwing the error about IMAP4_SSL. After looking into the issue, I believe it may have something to do with the mappings in Python, but it all looks correct to me. I also checked that there are no other Python versions installed that could cause a conflict.

Here are the paths for Python and the imaplib2 package

$ which python3
/usr/bin/python3

$ python3 --version
Python 3.8.5

$ whereis python3
'/usr/bin/python3.8-config'
'/usr/bin/python3'
'/usr/bin/python3.8'
'/usr/lib/python3'
'/usr/lib/python3.8'
'/usr/lib/python3.9'
'/etc/python3'
'/etc/python3.8'
'/usr/local/lib/python3.8'
'/usr/include/python3.8'
'/usr/share/python3'
'/usr/share/man/man1/python3.1.gz'

$ sys.path
''
'/usr/lib/python38.zip'
'/usr/lib/python3.8'
'/usr/lib/python3.8/lib-dynload'
'/home/ubuntu/.local/lib/python3.8/site-packages'
'/usr/local/lib/python3.8/dist-packages'
'/usr/lib/python3/dist-packages'
> import imaplib2
> imaplib2.__file__
'/home/ubuntu/.local/lib/python3.8/site-packages/imaplib2/__init__.py'

Solution

  • The problem is likely that you are using an outdated version of imaplib2 from PyPI. You can install a newer version on Ubuntu using the following command.

    sudo apt install python3-imaplib2
    

    (Alternatively, if you are not on Ubuntu, you can always install from source.)

    Depending on your PYTHONPATH and the location where you installed the old version of imaplib2, you may also need to uninstall the old version using pip before import imaplib2 will import the right version.

    python3 -m pip uninstall imaplib2