导入错误:当我尝试导入BigQuery时,没有名为google.cloud的模块

时间:2017-03-30 17:53:10

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

当我尝试在Google App Engine中使用此语句时

from google.cloud import big query

它产生错误No Module Named Cloud但它运行正常并且如果我从本地机器运行它则更新BigQuery表。

我想在Google App Engine上将其作为一项cron工作,但这不起作用。

环境: 我在标准环境及其Python 2.7中。我没有运行App Engine SDK开发服务器,我正在部署App live。

我的代码是:

 import time
 import uuid
 from google.cloud import bigquery
 client = bigquery.Client()
 jobid = "jid" + str(uuid.uuid4())
 query = "Select * FROM Table"
 job = client.run_async_query(jobid, query)

当我运行命令gcloud app browse时,它会打开一个浏览器并发出以下错误:

Error: Server Error 
The server encountered an error and could not complete your request.[![Error Trace][1]][1]
     

enter image description here

1 个答案:

答案 0 :(得分:1)

原因可能是App Engine Standard Environment尚不支持big-query客户端库。我遇到过这个问题。 Check Here。此cord中有更多有用的讨论。您可以在这里找到解决方案。

<强>更新

感谢您的评论和建议。为了改进上一篇文章,这里有一些额外的信息。由于big-query client library不支持App Engine Standard Environment,因此与big-query service进行通信的方法是使用googleapiclient

为了澄清这一点,以下是使用googleapiclient的步骤。

  1. Oauth2clientgoogleapiclient导入模块:

    from googleapiclient.discovery import build
    from oauth2client.client import GoogleCredentials
    
  2. 验证和构建服务:

    build('bigquery', 'v2', credentials=GoogleCredentials.get_application_default(), cache_discovery=False)
    
  3. 通过工作:此处使用流媒体示例。

    row_to_stream = {'keys':keys, 'f1':row['f1'], 'f2':row['f2'], 'ctr':row['ctr'], 'position':row['position']}                    
    insert_all_data = {
     "kind": "bigquery#tableDataInsertAllRequest",
     "skipInvaliedRows": True,
     "ignoreUnknownValues": True,
     'rows':[{
                    'insertId': str(uuid.uuid4()),
                    'json': row_to_stream,
                }]
     }
    
     build('bigquery', 'v2', cache_discovery=False).tabledata().insertAll(
     projectId=projectid,
     datasetId=dataset_id,
     tableId=tableid,
     body=insert_all_data).execute(num_retries=5)
    
  4. 希望这有用!