ImportError: No module named google.oauth2

2019-04-29 21:23发布

问题:

I wanted to add a google.cloud.storage dependency to my project so I tried to install this dependency with

pip install --upgrade google-cloud-storage

Running my app again with dev_appserver, it shows me that my gcloud components needed to be updated. Ok so, gcloud components update

And in my src/__init__.py file, I got the code that tells gcloud in which folder to look for dependencies like this:

from google.appengine.ext import vendor

vendor.add('src/libs')

All the dependencies are installed correctly, except that I'm getting the error ImportError: No module named google.oauth2

PS: My app is using OAuth2 to secure access to the API. And it was working correctly before I do a components update, now even if I rollback code, remove the libs folder and install again dependencies, I still got the No module error, and it seems like dev_appserver is not looking for that dependency inside the libs folder !

Here's the result of gcloud --version:

Google Cloud SDK 188.0.1
app-engine-python 1.9.66
app-engine-python-extras 1.9.63
bq 2.0.28
core 2018.02.08
gsutil 4.28

And here's the Traceback:

Traceback (most recent call last):
  File "/home/login/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/login/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/login/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 96, in LoadObject
    __import__(cumulative_path)
  File "/home/headless/Documents/Projects/meterFleet/app-backend/src/main.py", line 5, in <module>
    from src.app.user.api import UserApi
  File "/home/headless/Documents/Projects/meterFleet/app-backend/src/app/user/api.py", line 7, in <module>
    from src.googleapis.iam import getIamPolicy, addIapUser, deleteIapUser
  File "/home/headless/Documents/Projects/meterFleet/app-backend/src/googleapis/iam.py", line 5, in <module>
    from src.common.authentication import OAuth
  File "/home/headless/Documents/Projects/meterFleet/app-backend/src/common/authentication.py", line 3, in <module>
    from google.oauth2 import service_account
  File "/home/login/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/sandbox.py", line 1147, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named google.oauth2

回答1:

The problem was coming from the dependencies being installed in both the project folder (in the src/libs folder), and in the python local libs folder (/usr/local/python2.7/dist-packages). I removed the google libraries from the python libs folder and it's now working again !



回答2:

I've had this issue quite a bit. I uninstalled all Google packages from my local machine, deleted the lib folder in my GAE app folder, created it again then executed:

pip install -t lib google-auth google-auth-httplib2 google-api-python-client --upgrade

That should fix your problem.



回答3:

I decided to use pipenv to solve this issue. pip3 install pipenv pipenv install pipenv shell Then install the libraries you need.