python - 如何将 python 的谷歌云端点与现有的 GAE 项目集成?

标签 python google-app-engine google-cloud-endpoints

当我尝试将 Google Cloud 端点集成到现有项目中时,出现此错误:

 ImportError: No module named endpoints

我已经将端点添加到我的 app.yaml 文件中。端点 api 文件在外部与其自己的 app.yaml 文件一起工作,但在项目目录中运行时会出现错误。为了简单起见,我将所有 api 调用路由到“endpoints_api.py”。也许我错过了一些东西。

这是我的目录设置:

    -project
      -handlers
      -media
      -templates
      -webapp2_extras
      __init__.py
      app.yaml
      main.py
      endpoints_api.py

这是我的 app.yaml 文件:

    application: project-aplha
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: true

    handlers:
    # Endpoints Api
    - url: /_ah/spi/.*
      script: endpoints_api.APPLICATION

    - url: /favicon\.ico
      static_files: media/favicon.ico
      upload: media/favicon.ico

    - url: /media
      static_dir: media

    # Main Script
    - url: /.*
      script: main.APPLICATION

    libraries:
    - name: endpoints
      version: 1.0

    - name: webapp2
      version: latest

    - name: jinja2
      version: latest

    - name: pycrypto
      version: latest

以及处理程序类的示例(如果重要的话):

class SignupHandler(base.BaseHandler):
 def get(self):
    return self.render_template('sighup.html')

 def post(self):
    name = self.request.get('name')
    email = self.request.get('email')
    password = self.request.get('password')

也许还有endpoints_api.py 文件:

import endpoints
from google.appengine.ext import ndb
from protorpc import messages
from protorpc import message_types
from protorpc import remote


class Task(messages.Message):
  name = messages.StringField(1, required=True)
  owner = messages.StringField(2)

class TaskModel(ndb.Model):
  name = ndb.StringProperty(required=True)
  owner = ndb.StringProperty()

@endpoints.api(name='tasks', version='v1',
               description='API for Task Management')
class TaskApi(remote.Service):

  @endpoints.method(Task, Task,
                    name='task.insert',
                    path='task',
                    http_method='POST')
  def insert_task(self, request):
    TaskModel(name=request.name, owner=request.owner).put()
    return request

APPLICATION = endpoints.api_server([TaskApi])

最佳答案

我在 OS X 上使用最新的 AppEngine SDK 1.9.40 时遇到了同样的错误。endpoints 模块不在 Python 模块搜索路径中,因此当您的代码运行 import 时端点,它失败并出现 ImportError。

即使使用 GUI GoogleAppEngineLauncher 运行,该模块也未被引用。

在 OS X 上,该模块位于 /usr/local/google_appengine/lib/endpoints-1.0/endpoints。您可以通过将其添加到 PYTHONPATH 然后从解释器运行 import endpoints 来验证这一点。这应该可以工作,尽管它会失败,因为它有一个对“protorpc”的内部引用。

不幸的是,在命令行上,dev_appserver.py 无法引用 PYTHONPATH,因此您会继续收到以下命令的 ImportError:

dev_appserver.py --datastore_path=datastore.db 。

郑重声明,我将其包含在我的 app.yaml 中:

libraries:
- name: ssl
  version: latest
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest
- name: pycrypto
  version: "2.6"
- name: endpoints
  version: "1.0"

关于python - 如何将 python 的谷歌云端点与现有的 GAE 项目集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22655806/

相关文章:

java - 在每个请求上获取内存缓存变量会导致问题

google-app-engine - 在没有 Files API 的情况下从 urlfetch 保存图像

python - 强制 YAML 将字符串格式设置为带引号的字符串

java - 使用 Jython 将参数从 Java 传递到 Python

python - 当某些列列表值为空时如何合并包含列表值的列?

google-app-engine - 如何为 GCP VM 实例启用 SSL 连接 (Mautic)

python - 将列表拆分为 n 组的替代方法

google-app-engine - "message": "java.lang.NullPointerException" when trying to insert entity via api-explorer

java - 删除端点 url 中的 v1

google-app-engine - 从应用引擎端点提供 blob