【问题标题】:How do I integrate google cloud endpoints for python with an existing GAE project?如何将 Python 的谷歌云端点与现有的 GAE 项目集成?
【发布时间】:2014-03-26 08:57:21
【问题描述】:

当我尝试将谷歌云端点集成到现有项目中时,我收到此错误:

 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])

【问题讨论】:

  • 您还没有真正提供任何足够接近的信息让人们除了猜测之外做任何事情。例如,您是否在 app.yaml 中启用了端点,您在 app.yaml 中的处理程序是什么样的等等......
  • 上述上下文表明端点已添加到 app.yaml 文件中。我正在路由所有“/_ah/api/explorer”链接以使用endpoints_api.py 而不是main.py。在上面添加 app.yaml 和处理程序示例
  • 为什么你的APPLICATION是大写的?
  • 如何在本地运行项目?
  • 将应用程序更改为应用程序。还更改了措辞以使事情更清晰。

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


【解决方案1】:

我在 OS X 上使用最新的 AppEngine SDK 1.9.40 时遇到了同样的错误。endpoints 模块不在 Python 模块搜索路径中,因此当您的代码运行 import endpoints 时,它会因 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"

【讨论】:

    【解决方案2】:

    您必须使用最新版本的 Google Cloud SDK 运行您的项目。

    切换到您的项目文件夹并通过以下命令运行它:

    dev_appserver.py .

    【讨论】:

    • 端点 api 使用它自己的 app.yaml 文件,但当我与现有项目集成时不会运行。将更新应用引擎 sdk 并报告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2018-02-28
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多