简体   繁体   English

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

[英]No module named psycopg2._psycopg2 on Google Cloud Functions

I have a Google Cloud Function that uses Google Cloud SQL through psycopg2.我有一个通过 psycopg2 使用 Google Cloud SQL 的 Google Cloud Function。 I have psycopg2 in my requirements.txt but once the service updates I get the error No module named psycopg2._psycopg2 .我的 requirements.txt 中有 psycopg2,但是一旦服务更新,我就会收到错误No module named psycopg2._psycopg2

Is this a problem with psycopg2's C libraries running in the Cloud Function's environment ?这是在Cloud Function 环境中运行的 psycopg2 的 C 库的问题吗? How do I fix this?我该如何解决?

main.py主程序

import psycopg2

def postgres_demo(request):
    return "hi"

requirements.txt要求.txt

psycopg2==2.8.2

serverless.yml无服务器.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:运行serverless deploy总是会导致:

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"}

I solved it by removing python-serverless-requirements altogether and using the serverless package option to make sure only main.py and requirements.txt were included in the zip file.我通过完全删除python-serverless-requirements并使用无服务器package选项来确保只有main.pyrequirements.txt包含在 zip 文件中来解决它。

Google cloud installs the packages in requirements.txt itself so you don't need a serverless plugin.谷歌云在 requirements.txt 本身中安装包,因此您不需要无服务器插件。 Google cloud also has the necessary C libraries for psycopg2.谷歌云也有 psycopg2 必要的 C 库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM