无法导入模块' lambda_function':没有名为' psycopg2._psycopg的模块

发布于 2025-02-10 03:55:22 字数 529 浏览 1 评论 0 原文

我尝试使用Amazon RDS Postgres数据库调用Lambda功能。当我尝试运行lambda函数时,我遇到了这个错误:

    {
      "errorMessage": "Unable to import module 'lambda_function': No module named'psycopg2._psycopg'",
      "errorType": "Runtime.ImportModuleError",
      "requestId": "c1c7e814-9560-4d5e-a71d-fa39fc33132e",
      "stackTrace": []
    }

“

我已经添加 psycopy2--二进制包装。

I tried to invoke lambda function with Amazon RDS Postgres database. I got this error, when I tried to run the lambda function:

    {
      "errorMessage": "Unable to import module 'lambda_function': No module named'psycopg2._psycopg'",
      "errorType": "Runtime.ImportModuleError",
      "requestId": "c1c7e814-9560-4d5e-a71d-fa39fc33132e",
      "stackTrace": []
    }

enter image description here

I already add psycopy2-binary pack with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2025-02-17 03:55:22

逐线键入以下代码中的代码。可以用您选择的包装替换PIP安装pandas命令。您也可以安装超过1个软件包*。

mkdir folder
cd folder
virtualenv v-env
source ./v-env/bin/activate
pip3 install psycopg2
deactivate

然后按行键入以下代码以创建您的图层

mkdir python
cd python
cp -r ../v-env/lib64/python3.7/site-packages/* .
cd ..
zip -r psycopg2_layer.zip python
aws lambda publish-layer-version --layer-name psycopg2 --zip-file fileb://psycopg2_layer.zip --compatible-runtimes python3.7

请参阅下面的链接以获取Postgres层: -

”在此处输入图像说明”

=“ https://i.sstatic.net/2psaa.png” alt =“在此处输入图像说明”>

转到AWS lambda函数 - >创建图层

import psycopg2

db_endpoint = 'RDS-endpoint'
db_port = 5432
db_username = 'RDS-username'
db_password = 'RDS-password'
db_name = 'RDS-DB-name'

def lambda_handler(event, context):
  connection = psycopg2.connect(user=db_username, password=db_password, host=db_endpoint, database=db_name)
  cursor = connection.cursor()
  query = "SELECT version() AS version"
  cursor.execute(query)
  results = cursor.fetchone()
  print("results-----",results)
  cursor.close()
  connection.commit()
  return results

Type the following code line by line into the terminal at the bottom. The pip install pandas command can be replaced with a package of your choosing. You can also install more than 1 package*.

mkdir folder
cd folder
virtualenv v-env
source ./v-env/bin/activate
pip3 install psycopg2
deactivate

Then type the following code line by line to create your layer

mkdir python
cd python
cp -r ../v-env/lib64/python3.7/site-packages/* .
cd ..
zip -r psycopg2_layer.zip python
aws lambda publish-layer-version --layer-name psycopg2 --zip-file fileb://psycopg2_layer.zip --compatible-runtimes python3.7

Please refer below link for postgres layer:-
https://towardsdatascience.com/python-packages-in-aws-lambda-made-easy-8fbc78520e30

enter image description here

enter image description here

Go to AWS Lambda function -> create layer
enter image description here

import psycopg2

db_endpoint = 'RDS-endpoint'
db_port = 5432
db_username = 'RDS-username'
db_password = 'RDS-password'
db_name = 'RDS-DB-name'

def lambda_handler(event, context):
  connection = psycopg2.connect(user=db_username, password=db_password, host=db_endpoint, database=db_name)
  cursor = connection.cursor()
  query = "SELECT version() AS version"
  cursor.execute(query)
  results = cursor.fetchone()
  print("results-----",results)
  cursor.close()
  connection.commit()
  return results

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文