ModuleNotFoundError... No Module named Google

Viewed 8758

I'm trying to link my Google App to cloud storage so I can upload and store images. It worked just fine locally.

Now I'm getting this error: (showrooom is the name of my app, and create is the page containing the upload form)

ModuleNotFoundError at /showroom/create/
No module named 'google'

Following the documentation and previous answers, I used PIP to install google-cloud, google-cloud-vision, and google-cloud-storage.

Running pip now shows that google-cloud is installed in my Virtual Environment.

(torque) C:\code\torque>pip install google-cloud
Requirement already satisfied: google-cloud in c:\users\user\envs\torque\lib\site-packages (0.34.0)```

But checking in Python (still within the directory and virtualenv), it returns none:

>>> import google
>>> print(google.__file__)
None

Can anyone point me in the right direction?

There are a lot of interesting errors, so I'll post the full traceback (torque is the name of my app).


Request Method: POST
Request URL: https://torque-256805.appspot.com/showroom/create/

Django Version: 2.2.5
Python Version: 3.7.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'showroom',
 'django.contrib.humanize',
 'django.contrib.sites',
 'crispy_forms',
 'allauth',
 'allauth.account']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/env/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/env/lib/python3.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/env/lib/python3.7/site-packages/django/contrib/auth/mixins.py" in dispatch
  52.         return super().dispatch(request, *args, **kwargs)

File "/env/lib/python3.7/site-packages/django/views/generic/base.py" in dispatch
  97.         return handler(request, *args, **kwargs)

File "/env/lib/python3.7/site-packages/django/views/generic/edit.py" in post
  172.         return super().post(request, *args, **kwargs)

File "/env/lib/python3.7/site-packages/django/views/generic/edit.py" in post
  142.             return self.form_valid(form)

File "/srv/showroom/views.py" in form_valid
  98.                 images.save()

File "/env/lib/python3.7/site-packages/django/forms/models.py" in save
  669.         return self.save_existing_objects(commit) + self.save_new_objects(commit)

File "/env/lib/python3.7/site-packages/django/forms/models.py" in save_new_objects
  807.             self.new_objects.append(self.save_new(form, commit=commit))

File "/env/lib/python3.7/site-packages/django/forms/models.py" in save_new
  947.         return super().save_new(form, commit=commit)

File "/env/lib/python3.7/site-packages/django/forms/models.py" in save_new
  646.         return form.save(commit=commit)

File "/env/lib/python3.7/site-packages/django/forms/models.py" in save
  458.             self.instance.save()

File "/env/lib/python3.7/site-packages/django/db/models/base.py" in save
  741.                        force_update=force_update, update_fields=update_fields)

File "/env/lib/python3.7/site-packages/django/db/models/base.py" in save_base
  779.                 force_update, using, update_fields,

File "/env/lib/python3.7/site-packages/django/db/models/base.py" in _save_table
  870.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "/env/lib/python3.7/site-packages/django/db/models/base.py" in _do_insert
  908.                                using=using, raw=raw)

File "/env/lib/python3.7/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/env/lib/python3.7/site-packages/django/db/models/query.py" in _insert
  1186.         return query.get_compiler(using=using).execute_sql(return_id)

File "/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1334.             for sql, params in self.as_sql():

File "/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in as_sql
  1278.                 for obj in self.query.objs

File "/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in <listcomp>
  1278.                 for obj in self.query.objs

File "/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in <listcomp>
  1277.                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]

File "/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in pre_save_val
  1228.         return field.pre_save(obj, add=True)

File "/env/lib/python3.7/site-packages/django/db/models/fields/files.py" in pre_save
  288.             file.save(file.name, file.file, save=False)

File "/env/lib/python3.7/site-packages/django/db/models/fields/files.py" in save
  86.         name = self.field.generate_filename(self.instance, name)

File "/env/lib/python3.7/site-packages/django/db/models/fields/files.py" in generate_filename
  307.         return self.storage.generate_filename(filename)

File "/env/lib/python3.7/site-packages/django/utils/functional.py" in inner
  256.             self._setup()

File "/env/lib/python3.7/site-packages/django/core/files/storage.py" in _setup
  363.         self._wrapped = get_storage_class()()

File "/env/lib/python3.7/site-packages/django/core/files/storage.py" in get_storage_class
  358.     return import_string(import_path or settings.DEFAULT_FILE_STORAGE)

File "/env/lib/python3.7/site-packages/django/utils/module_loading.py" in import_string
  17.     module = import_module(module_path)

File "/opt/python3.7/lib/python3.7/importlib/__init__.py" in import_module
  127.     return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>" in _gcd_import
  1006. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load
  983. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
  953. <source code not available>

File "<frozen importlib._bootstrap>" in _call_with_frames_removed
  219. <source code not available>

File "<frozen importlib._bootstrap>" in _gcd_import
  1006. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load
  983. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
  953. <source code not available>

File "<frozen importlib._bootstrap>" in _call_with_frames_removed
  219. <source code not available>

File "<frozen importlib._bootstrap>" in _gcd_import
  1006. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load
  983. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
  965. <source code not available>

Exception Type: ModuleNotFoundError at /showroom/create/
Exception Value: No module named 'google'

Update

Shane below gave me the idea of updating the requirements.txt (refreezing the file). Now the error reads:

ModuleNotFoundError at /showroom/create/
No module named 'google.storage'

...

**/srv/showroom/views.py in form_valid
                images.save**

Here is a cut of my requirements.txt. (lots of google stuff there I've been trying out)

Django==2.2.5
django-allauth==0.40.0
django-crispy-forms==1.7.2
django-gcloud-storage==0.4.0
google-api-core==1.14.3
google-auth==1.6.3
google-cloud==0.34.0
google-cloud-core==1.0.3
google-cloud-storage==1.20.0
google-cloud-vision==0.39.0
google-resumable-media==0.4.1
googleapis-common-protos==1.6.0
grpcio==1.24.3
gunicorn==19.9.0
idna==2.8
isort==4.3.21
lazy-object-proxy==1.4.2
mccabe==0.6.1
mysqlclient==1.4.4
oauthlib==3.1.0
4 Answers

You need to run pip3 install (package name) in order for it to work with Python 3

No module named 'google.storage'

Error message indicates that the python env on which you are executing your application was not able to find the "google.storage" module (pretty straight forward). But this can be caused by several things, but the most probable is:

1.- You don't have the module installed (if your default Python env is Python 2, when you do "pip install" it install the package on Python 2 env).

note: you can check your default Python env by just running "python" command and checking which version initializes.

2.- If you are using virtualenv (like is advised) is also possible that the path to the module has not been exported properly.

Some troubleshooting advises:

  • first check that the python env on which you are executing the application has the path to "google" module (import only the google part and see if it finds it).

  • If it find the module, you can print the path to it and check if you actually have the storage module installed. Example:

    import google
    print(google.__file__) 
    
  • If you have the storage module then it would confirm that the path is not properly exported.

Run your command with sudo privilege. It worked for me.

Related