gmail login failure using python and imaplib

You can try to turn on this: https://www.google.com/settings/security/lesssecureapps This action solved the same problem for me.


If you want to avoid this error without compromising your account's security, use OAuth to authenticate. The protocol is documented here, and there is Python sample code that shows the use of XOAUTH2 with imaplib.

Independent of this, you should consider enabling two-step verification on your account to make it more secure. If you do, you can use an App Password to connect to IMAP, which might also avoid the above warning.