【问题标题】:ImportError: No module named cloud in AppEngine, pythonImportError:在 AppEngine 中没有名为 cloud 的模块,python
【发布时间】:2020-07-28 20:39:02
【问题描述】:

当我尝试仅使用库通过 appengine 执行我的 script.py 时遇到问题 “从 google.cloud 导入 bigquery”,我尝试了一些解决方案,但我没有成功的方法,我向您展示了我用来执行此脚本的脚本。

这是我的脚本:

import json
import datetime
import webapp2
from google.cloud import bigquery
from google.appengine.ext import vendor
vendor.add('lib')

filename = "example.json"

def date_format(time):
        if time.find(".") != -1:
            time = time[:time.find(".")]
        date = datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S")
        return date.strftime("%Y%m%d")


class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        with open(filename) as file:
            array = []
            jsonData = json.load(file)
            d = str(date_format(jsonData['e']))
            self.response.write('Hello AppEngine from script! :: '+d)

            client = bigquery.Client()

            QUERY = (           
                    'SELECT field FROM `table` WHERE date='+d+' LIMIT 10'
                    )
            query_job = client.query(QUERY)
            rows = query_job.result()
            for row in rows:
                print(row.field)


app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)

这是我的 app.yaml:

runtime: python27
api_version: 1
threadsafe: true

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

这是我的 requirements.txt:

google-api-python-client
google-cloud

我有一个包含 bigquery 库的 lib 目录:google_cloud_bigquery-1.24.0.dist-info。 和其他带有 google_cloud 的库

我不知道我的代码是否正确,因为我看到了一些解决方案,但任何东西都可以帮助我使用 appengine 运行脚本。

希望你能帮帮我。

这是python 3的脚本

import google.cloud.bigquery as bigquery
# [START gae_python37_app]
from flask import Flask
from google.appengine.ext import vendor
vendor.add('lib')


# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = Flask(__name__)

@app.route('/')
def hello():
    """Return a friendly HTTP greeting."""
    client = bigquery.Client()
    QUERY = (           
            'SELECT ev FROM `table` WHERE f="20200201" LIMIT 10'
            )
    query_job = client.query(QUERY)
    rows = query_job.result()
    for row in rows:
        return row.ev


if __name__ == '__main__':
    # This is used when running locally only. When deploying to Google App
    # Engine, a webserver process such as Gunicorn will serve the app. This
    # can be configured by adding an `entrypoint` to app.yaml.
    app.run(host='127.0.0.1', port=8080, debug=True)
# [END gae_python37_app]

【问题讨论】:

    标签: python google-app-engine google-cloud-platform google-bigquery google-app-engine-python


    【解决方案1】:

    您正在使用 App Engine Standard 的 Python 2.7 运行时。此运行时不允许您使用 requirements.txt 文件指定依赖项。

    相反,您应该按照本指南向供应商提供任何依赖项,例如 google-cloud-bigquery 以及您的应用程序:https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27

    您可能需要完全删除任何现有的lib 目录并重新开始。

    另外,请注意 google-cloud 包已弃用,您不应使用它。

    【讨论】:

    • 感谢您的回答,但是当我删除并安装 google-cloud 库时,出现其他错误... ImportError: No module named concurrent.futures,我搜索了此错误以尝试解决它pip install futures,但不可能,错误仍然出现在 appengine 中,我尝试使用 python 3 运行此脚本,但现在出现 ModuleNotFoundError: No module named 'google',我尝试使用 pip3 install google,但错误仍然出现。
    • 我现在看到您使用的是 App Engine Standard 的 Python 2.7 运行时。此运行时不允许您使用 requirements.txt 文件指定依赖项,请参阅 cloud.google.com/appengine/docs/standard/python/tools/…。我已经相应地更新了我的答案。
    猜你喜欢
    • 2016-07-04
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    相关资源
    最近更新 更多