Can't import Datastore from google.cloud

孤街浪徒 提交于 2019-12-24 11:15:06

问题


I'm trying to create a basic web app using Flask and Google Datastore to make myself to Google Cloud. Though, when I deploy my app, I get an Error 500, the details being that Python can't import Datastore : ImportError: No module named cloud.

Here is my app.yaml:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: .*
  script: main.app

libraries:
- name: jinja2
  version: "2.6"
- name: markupsafe
  version: "0.15"
- name: flask
  version: 0.12

My main.py starts like follows:

from __future__ import absolute_import

# Standard imports
import time
import logging
import json
import threading

# Flask framework
from flask import request
from flask import Flask

# Google Cloud features
from google.cloud import datastore
# the following replaces requests
from google.appengine.api import urlfetch

Finally, my requirements.txt is the following:

Flask
google-cloud
click

When I deploy my app (using gcloud app deploy) and get to my site, I get my error 500.

I don't understand why I can't use from google.cloud import datastore since it's what Google does in their tutorial... I must be missing something but I can't find what.

Any help would be appreciated.


回答1:


From Installing the client library:

pip install --upgrade google-cloud-datastore

The client library you need (to match your import statement) is google-cloud-datastore, but you don't have that listed in your requirements.txt file.

Side note: your app.yaml file indicates your app is a standard env GAE app one, for which you have the optimized Google Datastore NDB Client Library library available, already included in your SDK (if you don't have specific reasons for choosing the generic one instead).




回答2:


You do not specify env: flex so you are using Standard Environment. Here it is described for flex yaml and here it doesn’t appear for standard yaml.

This is important because google-cloud is not supported for Standard Environment. google-cloud’s repository:

These libraries currently do not run on Google App Engine Standard

Link mentioned by Dan Cornilescu about the NDB Client is an oficial solution for standard environment and few examples can be found in the oficial documentation:

https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/ndb and https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/multitenancy



来源:https://stackoverflow.com/questions/47378833/cant-import-datastore-from-google-cloud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!