Google Cloud Functions上没有名为psycopg2._psycopg2的模块

问题描述 投票:0回答:1

我有一个通过psycopg2使用Google Cloud SQL的Google Cloud功能。我的requirements.txt中有psycopg2但是一旦服务更新,我得到错误No module named psycopg2._psycopg2

这是在Cloud Function's environment中运行的psycopg2的C库的问题吗?我该如何解决?

main.py

import psycopg2

def postgres_demo(request):
    return "hi"

requirements.txt

psycopg2==2.8.2

serverless.yml

service: gcf-python-v2

provider:
    name: google
    stage: dev
    runtime: python37
    region: us-central1
    project: my-project
    credentials: ~/.gcloud/keyfile.json

plugins:
    - serverless-google-cloudfunctions
    - serverless-python-requirements

custom:
    pythonRequirements:
        pythonBin: python3

package:
    exclude:
        - node_modules/**
        - .gitignore
        - .git/**

functions:
    second:
        handler: postgres_demo
        events:
            - http: path

Running serverless deploy always results in:

Deployment failed: RESOURCE_ERROR

 {"ResourceType":"cloudfunctions.v1beta2.function","ResourceErrorCode":"400","ResourceErrorMessage":"Function failed on loading user code. Error message: Code in file main.py can't be loaded.\nDid you list all required modules in requirements.txt?\nDetailed stack trace: Traceback (most recent call last):\n  File \"/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py\", line 305, in check_or_load_user_function\n    _function_handler.load_user_function()\n  File \"/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py\", line 184, in load_user_function\n    spec.loader.exec_module(main)\n  File \"<frozen importlib._bootstrap_external>\", line 728, in exec_module\n  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n  File \"/user_code/main.py\", line 1, in <module>\n    import psycopg2\n  File \"/user_code/psycopg2/__init__.py\", line 50, in <module>\n    from psycopg2._psycopg import (                     # noqa\nModuleNotFoundError: No module named 'psycopg2._psycopg'\n"}
google-cloud-functions psycopg2 google-cloud-sql
1个回答
2
投票

我通过完全删除python-serverless-requirements并使用无服务器的package选项来确保只有main.pyrequirements.txt包含在zip文件中。

Google云会在requirements.txt中安装这些软件包,因此您不需要无服务器插件。 Google云还拥有psycopg2所需的C库。

© www.soinside.com 2019 - 2024. All rights reserved.