Python Gmail API setup fails with Module_six_moves_urllib_parse AttributeError

I’m getting a strange error when trying to set up the Gmail API in my Python project. The error message mentions something about Module_six_moves_urllib_parse not having the urlparse attribute. I think this might be related to package version conflicts but I’m not sure how to fix it.

I already tried updating the six package using pip but that didn’t help at all. The error happens when I run my authentication script.

Here’s the full error traceback:

Traceback (most recent call last):
  File "./gmail_setup.py", line 31, in <module>
    auth_credentials = execute(oauth_flow, TOKEN_STORAGE, http=http_client)
  File "/Library/Python/2.7/site-packages/oauth2client/util.py", line 141, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/oauth2client/old_run.py", line 124, in execute
    auth_url = oauth_flow.step1_get_authorize_url()
  File "/Library/Python/2.7/site-packages/oauth2client/util.py", line 141, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 1831, in step1_get_authorize_url
    return _update_query_params(self.auth_uri, query_params)
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 439, in _update_query_params
    parts = urllib.parse.urlparse(uri)
AttributeError: 'Module_six_moves_urllib_parse' object has no attribute 'urlparse'

What’s the best way to resolve this issue?

This happens when the six library and oauth2client don’t play nice together. The six library’s urllib.parse module isn’t importing correctly, so urlparse becomes unavailable. I hit this exact problem two years ago and fixed it by downgrading six to version 1.10.0. Try pip install six==1.10.0 to force the version that works with oauth2client. But honestly, since you’re on Python 2.7, I’d upgrade to Python 3 and switch to google-auth and google-auth-oauthlib instead. oauth2client is deprecated anyway, and the newer auth libraries handle dependencies way better without these six compatibility headaches.

I experienced a similar issue while working on an older Gmail integration. The conflict arises because oauth2client does not function well with the newer releases of the six library, particularly regarding the import of urllib.parse. To resolve it, I recommend setting up a new virtual environment and installing compatible versions like this: pip install oauth2client==4.1.3 six==1.15.0. This combination resolved the Module_six_moves_urllib_parse error for me. However, keep in mind that oauth2client has been deprecated, and transitioning to the google-auth library is advisable for better handling of dependencies without the need for six.

Same headache last month! Uninstall both six and oauth2client completely first: pip uninstall six oauth2client. Then reinstall oauth2client - it’ll pull in the right six version automatically. Worked for me when nothing else did.