Error importing modules in Python

0

I am developing an app with Angular for Front and Python (webapp2) as a backend all about App Engine.

At this point I need to use Google Oauth2 to request an authorization token.

According to Google's documentation. I have to import the corresponding modules

import google.oauth2.credentials
import google_auth_oauthlib.flow

Before I install them with pip

pip install --upgrade google-api-python-client
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2

It is mounted in a virtual environment. After running dev_appserver.py app.yaml everything is fine, but if I update the web it returns me:

Traceback (most recent call last):
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/home/nacho/altostratusCS/main.py", line 3, in <module>
    import google.oauth2.credentials
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/sandbox.py", line 1149, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named google.oauth2

Why should not the module be recognized if it is already installed with pip?

Thanks in advance

    
asked by Nacho 22.10.2018 в 12:34
source

2 answers

0

The solution in the Google Cloud documentation.

link

  • The first thing is to create a "lib" directory within the project.

  • Install the necessary libraries with pip install -t lib/ <library_name>

  • Create a file named appengine_config.pyen the same folder as your file app.yaml.

  • The newly created file must contain

  • # appengine_config.py
    from google.appengine.ext import vendor
    
    # Add any libraries install in the "lib" folder.
    vendor.add('lib')

    It finally worked for me after following these steps.

        
    answered by 22.10.2018 / 16:37
    source
    0

    Have you activated the virtual environment before installing the modules?

    The error may be because you are installing the modules outside of the virtual environment. Try activating the virtual environment and then execute the pip command.

        
    answered by 22.10.2018 в 16:09