【问题标题】:Python, AttributeError: module 'google.cloud.bigquery' has no attribute 'Client' after cx_Freeze buildPython,AttributeError:模块'google.cloud.bigquery'在cx_Freeze构建后没有属性'Client'
【发布时间】:2019-01-17 16:28:20
【问题描述】:

我正在尝试使用 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 软件包,但没有成功。任何指针将不胜感激。

【问题讨论】:

    标签: python google-bigquery cx-freeze


    【解决方案1】:

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

    您的本地开发环境似乎缺少 bigquery 库。确保在其中创建了 virtualenv 和 install 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)
    

    【讨论】:

      【解决方案2】:

      试试

      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)
      

      在你的主脚本中。

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

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

      【讨论】:

      • 使用第一个和第三个 sn-p,现在我收到以下错误:ModuleNotFoundError: No module named 'google.cloud.bigquery.client'
      • @thatguyoverthere 我不知道这是否有帮助:google 使用requests,您将在Requests library: missing SSL handshake certificates file after cx_Freeze 中找到有关如何使用requests 和cx_Freeze 的更多信息。另请参阅ModuleNotFoundError: No module named 'google' on python 3.6.7。
      • @thatguyoverthere 你能在本地安装中看到定义了Client 类的位置吗?它应该在模块[path_to_site_packages]/google/cloud/bigquery/client.py 中。然后你会在你的构建目录中看到相应的文件lib/google/cloud/bigquery/client.pyc 吗?
      • 是的,我可以在该路径中看到 client.py 文件,并且其中定义了 Client() 类。实际上,Python 代码在我粘贴和您提供的所有格式下都运行良好。这是 cx_Freeze 构建,它似乎没有将正确的项目打包在一起。
      • 我在 Google 库中粘贴了一个 init.py 文件,因为它丢失了。以前它根本找不到包,但不是它唯一的客户端。
      猜你喜欢
      • 2017-06-30
      • 1970-01-01
      • 2022-07-23
      • 2022-11-05
      • 2021-08-30
      • 2019-01-28
      • 2017-06-25
      • 1970-01-01
      • 2018-04-28
      相关资源
      最近更新 更多