【问题标题】:How to import BigQuery in AppEngine for Python如何在 AppEngine for Python 中导入 BigQuery
【发布时间】:2017-08-22 10:35:48
【问题描述】:

我正在尝试使用 Python 2.7 从 Google AppEngine(已部署)运行 BigQuery 查询,但我在 StackDriver 的错误报告中看到此错误:

ImportError: No module named cloud

这是我的代码(main.py):

from __future__ import absolute_import

import webapp2
from google.cloud import bigquery


class MainPage(webapp2.RequestHandler):
    def get(self):

        # Instantiates a client
        bigquery_client = bigquery.Client()

        # The name for the new dataset
        dataset_name = 'my_new_set'

        # Prepares the new dataset
        dataset = bigquery_client.dataset(dataset_name)

        # Creates the new dataset
        dataset.create()

        # Remove unwanted chars
        #self.response.write(str(container))


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

这是我的(app.yaml):

runtime: python27
api_version: 1
threadsafe: true

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

错误消息会让我假设 BigQuery 的库没有被导入。但是,如果这段代码是在 AppEngine 中部署的,那么该库不应该已经默认安装在 AppEngine 中了吗?


试图解决问题

尝试 #1

我发现这篇文章提到了类似的问题。建议是将这一行添加到文件的顶部。我在文件中添加了该行,但问题仍然存在:

from __future__ import absolute_import

来源: No module named cloud while using google.cloud import bigquery

尝试 #2

我在笔记本电脑本地安装了 BigQuery 的客户端:

pip install google-cloud-bigquery==0.22.1

我还在“lib”文件夹中安装了相同的客户端,以便在部署后将其上传到 AppEngine:

pip install --target='lib' google-cloud-bigquery==0.22.1

最后,还需要使用此内容创建一个名为“appengine_config.py”的文件:

# appengine_config.py
from google.appengine.ext import vendor

    # Add any libraries install in the "lib" folder.
    vendor.add('lib')

来源:https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27

但是,这种尝试也没有奏效。错误信息变为如下:

*File "/base/data/home/apps/p~experimenting-1130/2.400173726395247238/lib/httplib2/__init__.py", line 352: print('%s:' % h, end=' ', file=self._fp) ^ SyntaxError: invalid syntax
at <module> (/base/data/home/apps/p~experimenting-1130/2.400173726395247238/lib/google_auth_httplib2.py:23)
at <module> (/base/data/home/apps/p~experimenting-1130/2.400173726395247238/lib/google/cloud/_helpers.py:31)
at <module> (/base/data/home/apps/p~experimenting-1130/2.400173726395247238/lib/google/cloud/bigquery/_helpers.py:21)
at <module> (/base/data/home/apps/p~experimenting-1130/2.400173726395247238/lib/google/cloud/bigquery/__init__.py:26)
at get (/base/data/home/apps/p~experimenting-1130/2.400173726395247238/main.py:75)
at dispatch (/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:545)
at dispatch (/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:547)
at __call__ (/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:1077)
at default_dispatcher (/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:1253)
at __call__ (/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:1505)
at __call__ (/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:1511)*

如何在 AppEngine(已部署)中正确导入 BigQuery 库?

感谢您的帮助。

【问题讨论】:

  • 你的应用引擎项目中有 lib 文件夹吗?
  • 嗨,我的本地实例中有 lib 文件夹。所以,我想在我部署代码时它正在被上传到 AppEngine。我错过了什么吗?

标签: python-2.7 google-app-engine google-bigquery


【解决方案1】:

以下解决方案对我有用,而无需使用 from __future__ import absolute_import。需要遵循三个主要步骤。

1。将 google-api-python-client 和 google-cloud 复制到项目文件夹中

尽管这听起来违反直觉,但根据documentation

[...] Python 客户端库未安装在 App Engine Python 运行时环境中,[因此] 它们必须像第三方库一样被供应到您的应用程序中。

所以为了使用google.cloud,必须将库代码复制到项目的源目录中。库代码与应用程序代码一起上传到 App Engine。

要将库代码复制到您的项目中:

  1. 创建一个目录(例如 lib/)以将第三方库存储在项目的根文件夹中

    mkdir lib
    
  2. 将 google-api-python-client 和 google-cloud 库复制到您刚刚创建的文件夹中。我在下面的例子中使用 pip。

    pip install -t lib/ --upgrade google-api-python-client
    pip install -t lib/ --upgrade google-cloud
    

2。将已安装的库链接到应用程序

  1. 在与您的 app.yaml 文件相同的文件夹中创建一个名为 appengine_config.py 的文件
  2. 编辑appengine_config.py 并包含以下代码

    # appengine_config.py
    from google.appengine.ext import vendor
    
    # Add any libraries install in the "lib" folder.
    vendor.add('lib') 
    

3。在 requirements.txt 中包含添加的库

  1. 编辑您的 requirements.txt 文件并包含您添加的库的名称

    # other requirements
    google-api-python-client
    google-cloud
    

部署应用后,您现在应该可以毫无问题地使用from google.cloud import bigquery

欲了解更多信息,请参阅using third-party libraries

【讨论】:

猜你喜欢
  • 2011-10-25
  • 1970-01-01
  • 2018-05-27
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多