ImportError: No module named google.oauth2
Asked Answered
R

7

18

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
Radarman answered 14/2, 2018 at 17:7 Comment(0)
R
4

EDIT: I think you never get this error if you use something like virtualenv.

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 !

Radarman answered 16/2, 2018 at 13:13 Comment(1)
With me the google packages are in .local/lib/python2.7/site-packages. It probably depends on how you installed python/pip. Which packages did you remove? Can any harm be done to your system by removing packages?Caulis
L
13

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.

Livelong answered 1/5, 2018 at 12:20 Comment(0)
B
6

Just upgrading some python packages solved my issue:

pip install --upgrade google-auth google-auth-httplib2 google-api-python-client
Barbera answered 23/5, 2020 at 9:56 Comment(0)
R
4

EDIT: I think you never get this error if you use something like virtualenv.

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 !

Radarman answered 16/2, 2018 at 13:13 Comment(1)
With me the google packages are in .local/lib/python2.7/site-packages. It probably depends on how you installed python/pip. Which packages did you remove? Can any harm be done to your system by removing packages?Caulis
E
3

In my case it was because the file itself was called google.py, so it was trying to import itself.

Edwinaedwine answered 1/11, 2022 at 15:33 Comment(0)
E
1

I decided to use pipenv to solve this issue.

pip3 install pipenv
pipenv install
pipenv shell

Then install the libraries you need.

Elaina answered 25/12, 2018 at 1:20 Comment(0)
R
1

I had virtualenv created and was trying to run a script where you insert data into google cloud bigquery with a service account and automate it using cron file , the solution for me was to replace usr/local/bin/python to my virtualenv python path.

old cron file :

30 11 * * * /usr/local/bin/python3 /opt/deployment/cronJobs/ProjectAssetMetrics/daily/updateCCMSCountJob.py > /opt/deployment/cronJobs/ProjectAssetMetrics/daily/updateCCMSCountJob.log 2>&1 in your

new cron file :

16 12 * * * /opt/.envs/sl_dw/bin/python3 /opt/deployment/cronJobs/ProjectAssetMetrics/daily/updateCCMSCountJob.py > /opt/deployment/cronJobs/ProjectAssetMetrics/daily/updateCCMSCountJob.log 2>&

You can check your python path by typing:

$ which python

make sure you activate your virtualenv if any

Rrhoea answered 28/6, 2023 at 7:1 Comment(0)
J
0

In case none of the above works for you - I found this answer from GitHub by hiranya911. It solved my problem perfectly.

Jospeh answered 9/11, 2020 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.