title: gmail login failure using python and imaplib
body: <p>I'm seeking some help logging into a gmail account and downloading some emails using a python script. I'm trying to adapt an approach found <a href="https://stackoverflow.com/questions/348630/how-can-i-download-all-emails-with-attachments-from-gmail">here</a>, but I'm running into a problem  with step 1, accessing the account via imap. </p>
  
  <p>here is the code I'm starting with: </p>
  
  <pre><code>import email
  import imaplib
  
  m = imaplib.IMAP4_SSL("imap.gmail.com",993)
  rc, resp = m.login('myemailaddress','mypassword')
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "email.py", line 1, in &lt;module&gt;
      import email, imaplib
    File "/home/will/wd/email.py", line 14, in &lt;module&gt;
       m.login('myemailaddress','mypassword')
    File "/usr/lib/python3.4/imaplib.py", line 538, in login
      raise self.error(dat[-1])
   imaplib.error: b'[ALERT] Please log in via your web browser: http://support.google.com/mail/accounts/bin/answer.py?answer=78754 (Failure)'
  </code></pre>
  
  <p>Imap is indeed enabled in gmail settings. I have looked at the instruction on the google support link and questions regarding this error in similar situations, such as <a href="https://stackoverflow.com/questions/5479240/python-imaplib-error">here</a> and <a href="https://stackoverflow.com/questions/10013736/how-can-i-avoid-google-mail-server-asking-me-to-log-in-via-browser">here</a>, but my situation is different from the first because <br>1) it never worked to start with, and<br> 2) I'm not running it so frequently to get blocked. Its also different from the second example because this is a normal gmail account, not a custom domain with a google apps account. <br>Using the <a href="https://accounts.google.com/DisplayUnlockCaptcha" rel="noreferrer">https://accounts.google.com/DisplayUnlockCaptcha</a> to try and allow access doesn't work for me either.</p>
  
  <p>The only thing that allows login to work is to change my google accounts security settings to "allow access for less secure apps". </p>
  
  <p>My question is: how can I fix my code (or my setup more generally) to allow me to login, without relaxing my accounts security settings? Is there some way to meet security requirements using libimap?</p>