Google BigQuery导入错误在部署云功能时



我有一个python云功能代码,该代码从GCS中读取.txt文件,解析并将行写入BigQuery。当我尝试将此云功能部署到MacOS中时,它给了我以下错误

我已经在GCP项目中验证了BigQuery API。

gcloud函数部署sql_upload--runtime python37- trigger-bucket test-bucket - entry-point load_sql

Deploying function (may take a while - up to 2 minutes)...failed.                                                                                                                                          
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: Code in file main.py can't be loaded.
Detailed stack trace: Traceback (most recent call last):
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 305, in check_or_load_user_function
    _function_handler.load_user_function()
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 184, in load_user_function
    spec.loader.exec_module(main)
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed  File "/user_code/main.py", line 24, in <module>
    from google.cloud import bigquery
ImportError: cannot import name 'bigquery' from 'google.cloud' (unknown location)

python中的依赖项由PIP管理,并在称为unignts.txt的元数据文件中表达。该文件必须与包含您功能代码的main.py文件处于同一目录。

如果此代码在您的计算机上工作,则只需创建sumplion.txt文件:

pip freeze > requirements.txt

否则,您首先必须安装BigQuery的依赖项,然后创建需求文件:

pip install --upgrade google-cloud-bigquery
pip freeze > requirements.txt

请参阅文档https://cloud.google.com/functions/docs/writing/specifying-depperencies-pythonhttps://cloud.google.com/bigquery/docs/reference/libraries#client-libraries-install-python

最新更新