【发布时间】: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