【问题标题】:google-app-engine fails to run Cron job and gives an ImportError: No module named gcloudgoogle-app-engine 无法运行 Cron 作业并给出 ImportError: No module named gcloud
【发布时间】:2017-01-25 03:53:44
【问题描述】:

app-engine 无法导入 gcloud 使用 gcloud app deploy app.yaml \cron.yaml 部署在 google app engine 上

在浏览器上打开并获取:

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/s~gcp-project-01/20160916t160552.395688991947248655/main.py", line 18, in <module>
    import update_datastore as ud
  File "/base/data/home/apps/s~vehicle-monitors-api/20160916t160552.395688991947248655/update_datastore.py", line 20, in <module>
    from gcloud import datastore, logging
ImportError: No module named gcloud

app.yaml 文件:

    runtime: python27
    api_version: 1
    threadsafe: true

handlers: 
- url: /
  script: main
  login: admin

cron.yaml 文件:

 cron:
    - description: run main app
      url: /
      target: main
      schedule: every 2 minutes

requirements.txt 文件:

   gcloud==0.14.0

【问题讨论】:

  • 您是否曾经通过需求文件进行安装?例如pip install -t lib -r requirements.txt ?
  • @GAEfan 即使在使用您之前提到的 pip 命令安装需求文件后,我也会遇到同样的错误。
  • 可能性太多。你的文件树是什么样的? gcloud 在您的 sys.path 中吗?从 cron 中删除 target:main,因为这是不正确的。
  • gcloud 需要加入到您的应用中:cloud.google.com/appengine/docs/python/tools/…
  • 同样在你的app.yaml 中你理论上应该有script: main.app 而不是script: main (从追溯看来,即使没有它,GAE 也能找到它,但我个人不会依赖它) .

标签: python google-app-engine cron gcloud-python google-cloud-python


【解决方案1】:

所有第 3 方软件包都需要安装在与您的应用相同的目录中。从应用程序的根目录运行它来安装它。

pip install gcloud -t .

【讨论】:

【解决方案2】:

搞定了! 使用路径:

import sys
sys.path.insert(0, 'lib')

补充:
还需要在需求中添加protobuf: protobuf==3.1.0.post1

在谷歌文件夹中创建__init__.py

# this is a namespace package
try:
    import pkg_resources
    pkg_resources.declare_namespace(__name__)
except ImportError:
    import pkgutil
    __path__ = pkgutil.extend_path(__path__, __name__)

也可以使用pip install -t lib --upgrade protobuf

gcloud==0.18.1 已使用。

抱歉,迟到了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多