Search code examples
pythongoogle-drive-apihttplib2

httplib2.IncompleteRead: AttributeError: 'module' object has no attribute 'IncompleteRead'


I've been using a script that was no longer maintained, which downloads your entire Google Drive to your local storage. I mananged to fix a few issues to do with depreciation, and the script seemed to be running smoothly, however, as seemingly random times in my script, it will break, and I will receive the following error.

File "drive.py", line 169, in download_file
    except httplib2.IncompleteRead:
AttributeError: 'module' object has no attribute 'IncompleteRead'

These are the modules I am using

import gflags, httplib2, logging, os, pprint, sys, re, time
import pprint


from apiclient.discovery import build
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError, flow_from_clientsecrets
from oauth2client.tools import run_flow

And here is the code that is causing the error

 if is_google_doc(drive_file):
    try:
        download_url = drive_file['exportLinks']['application/pdf']
    except KeyError:
        download_url = None
else:
    download_url = drive_file['downloadUrl']
if download_url:
    try:
        resp, content = service._http.request(download_url)
    except httplib2.IncompleteRead:
        log( 'Error while reading file %s. Retrying...' % drive_file['title'].replace( '/', '_' ) )
        print 'Error while reading file %s. Retrying...' % drive_file['title'].replace( '/', '_' )
        download_file( service, drive_file, dest_path )
        return False
    if resp.status == 200:
        try:
            target = open( file_location, 'w+' )
        except:
            log( "Could not open file %s for writing. Please check permissions." % file_location )
            print "Could not open file %s for writing. Please check permissions." % file_location 
            return False
        target.write( content )
        return True
    else:
        log( 'An error occurred: %s' % resp )
        print 'An error occurred: %s' % resp
        return False
else:
    # The file doesn't have any content stored on Drive.
    return False

I am assuming this error has something to do with losing connection while downloading, and I am unfamilar with the httplib2 module.

The full code can be found here

Thankyou in advance to anyone who can shed some light in a possible fix.


Solution

  • I've been updating that drive backup script, and have encountered the same error. I haven't yet worked out what exception is being thrown, but in order to reveal what it is (and allow the script to keep running) I've made the following change:

    Remove this:

    -        except httplib2.IncompleteRead:
    -            log( 'Error while reading file %s. Retrying...' % drive_file['title'].replace( '/', '_' ) )
    

    Replace it with this:

    +        except Exception as e: #httplib2.IncompleteRead: # no longer exists
    +            log( traceback.format_exc(e) + ' Error while reading file %s. Retrying...' % drive_file['title'].replace( '/', '_' ) )
    

    This does have the downside that if it encounters an exception consistently, it may enter an endless loop. However, it will then reveal the actual exception being thrown, so the "except:" can be updated appropriately.

    This change is visible in the repository here.

    If I encounter the error again I'll update this answer with more detail.