Python,AttributeError:cx_Freeze构建后,模块“ google.cloud.bigquery”没有属性“ Client”

时间:2019-01-17 16:28:21

标签: python google-bigquery cx-freeze

我正在尝试使用cx_Freeze创建脚本的可执行文件,该脚本从同一文件夹加载.sql查询,在BigQuery DB上执行该查询,并返回检索到的数据的.csv。

  This is what the 'conda list' command gives me:
  google-api-core           1.1.0                      py_0    conda-forge
  google-auth               1.6.2                      py_0    conda-forge
  google-cloud-bigquery     1.8.1                      py_0    conda-forge  
  google-cloud-core         0.28.1                     py_0    conda-forge
  google-resumable-media    0.3.1                      py_0    conda-forge
  googleapis-common-protos  1.5.5                      py_0    conda-forge

脚本在第一行崩溃(在脚本中指定了KEY和PROJECT_ID,但出于安全原因未粘贴)

from google.cloud import bigquery
client = bigquery.Client.from_service_account_json(KEY, project=PROJECT_ID)

我的cx_Freeze的setup.py看起来像这样:

from cx_Freeze import setup, Executable

setup(name='output_script', executables = [Executable("my_script.py")], version="1.0.0",
options={
         "build exe":{"packages":["google.cloud.bigquery, google.cloud.bigquery.client"]}})

构建成功执行,但是当我在构建文件夹中运行my_script.exe时,出现以下错误:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\ProgramData\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "my_script.py", line 15, in <module>
  File "my_script.py", line 7, in queryBigQ
AttributeError: module 'google.cloud.bigquery' has no attribute 'Client'

尝试重新安装和更新所有Google软件包,但未成功。任何指针将不胜感激。

2 个答案:

答案 0 :(得分:1)

在冻结脚本之前,您是否尝试成功运行脚本?

您的本地开发环境似乎缺少bigquery库。确保在其中创建一个virtualenvinstall bigquery library

使用简单的脚本测试与BigQuery的连接:

from google.cloud import bigquery

client = bigquery.Client()

QUERY = (
    'SELECT * FROM `[PROJECT_ID].[BQ_INSTANCE].[BQ_TABLE]`'
)
query_job = client.query(QUERY)
rows = query_job.result()

for row in rows:
    print(row)

答案 1 :(得分:0)

尝试

from google.cloud.bigquery.client import Client
client = Client.from_service_account_json(KEY, project=PROJECT_ID)

或者也许

from google.cloud.bigquery import Client
client = Client.from_service_account_json(KEY, project=PROJECT_ID)

在您的主脚本中。

还尝试将options脚本中的setup.py参数替换为

options={"build exe": {"packages": ["google"]}}