Python谷歌云函数不会安装Pandas

2024-03-29 13:06:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我将部署一个Python函数作为Google云函数。它在本地测试良好,并毫无怨言地部署到GCP。然而,当我实际执行它时,它会崩溃

Error: function terminated. Recommended action: inspect logs for termination reason. Details:
The pandas library is not installed, please install pandas to use the to_dataframe() function.

My requirements.txt如下所示(并且已验证在部署该功能时它实际上正在上载)

appdirs==1.4.3
APScheduler==3.6.3
beautifulsoup4==4.8.2
cachetools==4.0.0
certifi==2019.11.28
chardet==3.0.4
click==7.1.1
distlib==0.3.0
filelock==3.0.12
Flask==1.1.1
google-api-core==1.16.0
google-api-python-client==1.8.0
google-auth==1.12.0
google-auth-httplib2==0.0.3
google-cloud-bigquery==1.24.0
google-cloud-core==1.3.0
google-cloud-storage==1.26.0
google-resumable-media==0.5.0
googleapis-common-protos==1.51.0
grpcio==1.27.2
httplib2==0.17.0
idna==2.9
itsdangerous==1.1.0
Jinja2==2.11.1
MarkupSafe==1.1.1
numpy==1.18.2
pandas==1.0.3
pipenv==2018.11.26
protobuf==3.11.3
pyasn1==0.4.8
pyasn1-modules==0.2.8
python-dateutil==2.8.1
pytz==2019.3
requests==2.23.0
rsa==4.0
six==1.14.0
soupsieve==2.0
tzlocal==2.0.0
uritemplate==3.0.1
urllib3==1.25.8
virtualenv==20.0.15
virtualenv-clone==0.5.4
Werkzeug==1.0.1
wget==3.2

下面是云函数日志中的一些详细信息

 severity: "ERROR"  
 textPayload: "Traceback (most recent call last):
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 383, in run_background_function
    _function_handler.invoke_user_function(event_object)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 214, in call_user_function
    event_context.Context(**request_or_event.context))
  File "/user_code/main.py", line 27, in sync_nyt_counties
    macdata()
  File "/user_code/main.py", line 108, in macdata
    df = query_job.to_dataframe()
  File "/env/local/lib/python3.7/site-packages/google/cloud/bigquery/job.py", line 3374, in to_dataframe
    create_bqstorage_client=create_bqstorage_client,
  File "/env/local/lib/python3.7/site-packages/google/cloud/bigquery/table.py", line 1706, in to_dataframe
    raise ValueError(_NO_PANDAS_ERROR)
ValueError: The pandas library is not installed, please install pandas to use the to_dataframe() function.
" 

我在拔头发!还有想法

谢谢

更新

为了消除其他可能的影响,我创建了一个最小的函数来演示这个问题。 以前,我只会在Google BigQuery API试图使用pandas时在执行函数时看到错误。现在,通过在main.py中添加一个导入,我将问题转移到了最前面。现在我在尝试部署函数时失败了(不必再等到运行时)

main.py

import pandas as pd

def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        return f'Hello World!'

requirements.txt

pandas

部署命令

gcloud functions deploy hello_world --runtime python37 --trigger-http

错误

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.
Did you list all required modules in requirements.txt?
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 1, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'

gcloud版本

Google Cloud SDK 287.0.0
alpha 2019.05.17
beta 2019.05.17
bq 2.0.56
core 2020.03.30
gsutil 4.49

补充说明:

  • 我在Windows10上运行这个
  • 我想我的GCP项目可能有点不对劲,所以我尝试将其部署到另一个项目中。同样的结果

是否可能是与客户端相关的问题?我不这么认为,但我是Python的新手,我觉得我在某个时候对Python安装做了一些奇怪的事情,我不知道云SDK在部署时是否在幕后使用了一些


Tags: toinpyenvcloudpandasrequestlib