google bigquery导入错误,同时部署云功能

时间:2019-06-25 21:31:11

标签: google-bigquery

我有一个python Cloud函数代码,该函数从GCS读取.txt文件,对其进行解析并将行写入bigquery。当我尝试从MacOS将此云功能部署到Google云中时,出现以下错误

我已确认在我的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)

1 个答案:

答案 0 :(得分:1)

Python中的依赖项通过pip进行管理,并在名为requirements.txt的元数据文件中表示。该文件必须与包含您的功能代码的main.py文件位于同一目录中。

如果此代码在您的计算机上正常运行,则只需通过以下方式创建requirements.txt文件:

pip freeze > requirements.txt

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

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

查看文档 https://cloud.google.com/functions/docs/writing/specifying-dependencies-python https://cloud.google.com/bigquery/docs/reference/libraries#client-libraries-install-python