Python 2.7 如何在AppEngine for Python中导入BigQuery

Python 2.7 如何在AppEngine for Python中导入BigQuery,python-2.7,google-app-engine,google-bigquery,Python 2.7,Google App Engine,Google Bigquery,我试图使用Python 2.7从Google AppEngine(已部署)运行BigQuery查询,但我在StackDriver的错误报告中看到了以下错误: 导入错误:没有名为cloud的模块 这是我的代码(main.py): 这是我的(app.yaml): 错误消息将使我假设没有导入BigQuery的库。但是,如果此代码部署在AppEngine中,那么默认情况下库不应该已经安装在AppEngine中吗 试图解决问题 尝试#1 我发现这篇文章提到了一个类似的问题。建议将这一行添加到文件的顶部。

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

导入错误:没有名为cloud的模块

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

这是我的(app.yaml):

错误消息将使我假设没有导入BigQuery的库。但是,如果此代码部署在AppEngine中,那么默认情况下库不应该已经安装在AppEngine中吗


试图解决问题

尝试#1

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

from __future__ import absolute_import
资料来源:

尝试#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')
资料来源:

然而,这一尝试也没有成功。错误消息更改为以下内容:

*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)*
*File”/base/data/home/apps/p~experioning-1130/2.400173726395247238/lib/httplib2/_init__.py”,第352行:打印(“%s:'%h,end=”,File=self.\u fp)^SyntaxError:无效语法
位于(/base/data/home/apps/p~experioning-1130/2.400173726395247238/lib/google_auth_httplib2.py:23)
位于(/base/data/home/apps/p~expericing-1130/2.400173726395247238/lib/google/cloud/_helpers.py:31)
位于(/base/data/home/apps/p~expericing-1130/2.400173726395247238/lib/google/cloud/bigquery/_helpers.py:21)
位于(/base/data/home/apps/p~experioning-1130/2.400173726395247238/lib/google/cloud/bigquery/_____.py:26)
获取时(/base/data/home/apps/p~experimenting-1130/2.400173726395247238/main.py:75)
发送时(/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:545)
发送时(/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:547)
在调用时(/base/data/home/runtimes/python27/python27\u lib/versions/third\u party/webapp2-2.3/webapp2.py:1077)
默认情况下,_dispatcher(/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py:1253)
在调用时(/base/data/home/runtimes/python27/python27\u lib/versions/third\u party/webapp2-2.3/webapp2.py:1505)
在调用时(/base/data/home/runtimes/python27/python27\u lib/versions/third\u party/webapp2-2.3/webapp2.py:1511)*
如何在AppEngine(已部署)中正确导入BigQuery库


感谢您的帮助。

以下解决方案对我有效,而无需使用来自未来导入绝对导入的
。有三个主要步骤要遵循

1.将google api python客户端和google云复制到项目文件夹中 尽管这听起来有悖常理

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

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

要将库代码复制到项目中,请执行以下操作:

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

    mkdir lib
    
  • 将google api python客户端和google云库复制到刚才创建的文件夹中。我在下面的示例中使用pip

    pip install -t lib/ --upgrade google-api-python-client
    pip install -t lib/ --upgrade google-cloud
    
  • 2.将已安装的库链接到应用程序
  • 在与
    app.yaml
    文件相同的文件夹中创建一个名为
    appengine\u config.py
    的文件
  • 编辑
    appengine\u 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
  • 编辑您的
    requirements.txt
    文件,并包括添加的库的名称

    # other requirements
    google-api-python-client
    google-cloud
    
  • 部署应用程序后,您现在应该可以使用google.cloud import bigquery中的
    ,而不会出现任何问题


    有关详细信息,请参见

    您的应用程序引擎项目中是否有lib文件夹?您好,我的本地实例中有lib文件夹。因此,我假设在部署代码时,它被上传到AppEngine。我遗漏了什么吗?名单
    # appengine_config.py
    from google.appengine.ext import vendor
    
    # Add any libraries install in the "lib" folder.
    vendor.add('lib') 
    
    # other requirements
    google-api-python-client
    google-cloud