我有一个通过psycopg2使用Google Cloud SQL的Google Cloud功能。我的requirements.txt中有psycopg2但是一旦服务更新,我得到错误No module named psycopg2._psycopg2
。
这是在Cloud Function's environment中运行的psycopg2的C库的问题吗?我该如何解决?
import psycopg2
def postgres_demo(request):
return "hi"
psycopg2==2.8.2
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
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"}
我通过完全删除python-serverless-requirements
并使用无服务器的package
选项来确保只有main.py
和requirements.txt
包含在zip文件中。
Google云会在requirements.txt中安装这些软件包,因此您不需要无服务器插件。 Google云还拥有psycopg2所需的C库。