flask app engine does not work with firestore?

Viewed 643

Setup: Run in Google Cloud Shell Standard Env

I tried to access firestore from the flask app using from google.cloud import firestore

I have installed it using pip install --upgrade google-cloud-firestore -t lib. If I run the script manually, it works fine. But when using dev_appserver.py, it fails. Got the error below.

$ dev_appserver.py app.yaml
INFO     2017-10-06 07:34:35,301 devappserver2.py:105] Skipping SDK update check.
INFO     2017-10-06 07:34:35,391 api_server.py:300] Starting API server at: http://0.0.0.0:34796
WARNING  2017-10-06 07:34:35,391 dispatcher.py:312] Your python27 micro version is below 2.7.12, our current production version.
INFO     2017-10-06 07:34:35,440 dispatcher.py:251] Starting module "default" running at: http://0.0.0.0:8080
INFO     2017-10-06 07:34:35,441 admin_server.py:116] Starting admin server at: http://0.0.0.0:8000

ERROR    2017-10-06 07:34:42,266 wsgi.py:263]
Traceback (most recent call last):
  File "/google/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/google/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/google/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/home/user1/projects/probfe/main.py", line 10, in <module>
    from google.cloud import firestore
  File "/google/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/sandbox.py", line 1132, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named google.gax

However, in my lib, I can see them.

$ls lib
builtins                       future-0.16.0.dist-info                         itsdangerous.pyc                requests-2.18.4.dist-info
cachetools                     futures-3.1.1.dist-info                         jinja2                          rsa
cachetools-2.0.1.dist-info     google                                          Jinja2-2.9.6.dist-info          rsa-3.4.2.dist-info
certifi                        googleapis_common_protos-1.5.3.dist-info        libfuturize                     setuptools
certifi-2017.7.27.1.dist-info  googleapis_common_protos-1.5.3-py2.7-nspkg.pth  libpasteurize                   setuptools-36.5.0.dist-info
chardet                        google_auth-1.1.1.dist-info                     _markupbase                     six-1.11.0.dist-info
chardet-3.0.4.dist-info        google_auth-1.1.1-py2.7-nspkg.pth               markupsafe                      six.py
click                          google_cloud_core-0.27.1.dist-info              MarkupSafe-1.0.dist-info        six.pyc
click-6.7.dist-info            google_cloud_core-0.27.1-py3.6-nspkg.pth        past                            socketserver
concurrent                     google_cloud_firestore-0.27.0.dist-info         pkg_resources                   tests
copyreg                        google_cloud_firestore-0.27.0-py3.6-nspkg.pth   ply                             _thread
dill                           google_gax-0.15.15.dist-info                    ply-3.8.dist-info               tkinter
dill-0.2.7.1.dist-info         google_gax-0.15.15-py2.7-nspkg.pth              protobuf-3.4.0.dist-info        urllib3
_dummy_thread                  grpc                                            protobuf-3.4.0-py2.7-nspkg.pth  urllib3-1.22.dist-info
easy_install.py                grpcio-1.4.0.dist-info                          pyasn1                          werkzeug
easy_install.pyc               html                                            pyasn1-0.3.7.dist-info          Werkzeug-0.12.2.dist-info
enum                           http                                            pyasn1_modules                  winreg
enum34-1.1.6.dist-info         idna                                            pyasn1_modules-0.1.4.dist-info  xmlrpc
flask                          idna-2.6.dist-info                              queue
Flask-0.12.2.dist-info         itsdangerous-0.24.dist-info                     reprlib
future                         itsdangerous.py                                 requests
1 Answers

Since you're using dev_appserver.py it means you have a standard env application. And in the standard env all external dependencies need to be installed in your app, not on the local python instalation (which is what you did with your pip invocation).

From Installing a third-party library:

  1. Create a directory to store your third-party libraries, such as lib/.

    mkdir lib
    
  2. Use pip (version 6 or later) with the -t <directory> flag to copy the libraries into the folder you created in the previous step. For example:

    pip install -t lib/ <library_name>
    
Related