Recently while working on an AWS Lambda function written in Python which uses PostgreSQL connection, I faced the following error.
No module named ‘psycopy2._psycopg’
The program was working properly in my laptop. I have used the most popular Postgres python package psycopg2 as the dependency in my program which works well in my laptop and other projects. But the same package is not working on AWS Lambda.
Root Cause
The psycopg2 python package requires Postgres libraries which is missing in AMI used by the AWS Lambda function.
Solution
There are multiple solutions to this. The easiest solution is to use a pre-compiled package aws-psycopg2 which has the required libraries. The package is present in the PyPi repository.
pip install aws-psycopg2
After this, just do the import of psycopg2 as usual in your program. Sample import is given below.
import psycopg2
With this package, my Lambda function started working perfectly.
I hope this tip is useful. Feel free to comment if you have any queries or feedbacks.
the install works but i can’t import in my python code nor in lambda
import aws-psycopg2
import aws-psycopg2
^
SyntaxError: invalid syntax
Your import is wrong.
Just do the below import. It should work.
import psycopg2
Have you bundled this package with the code and uploaded to lambda. Please explain the method that you used to bundle the dependent packages
I deployed the lambda into AWS and added the package to the vendored folder, but I am still getting the same error.
{“errorMessage”: “Unable to import module ‘postHandler’: No module named ‘psycopg2._psycopg’”, “errorType”: “Runtime.ImportModuleError”, “requestId”: “68ab0f02-4009-449e-b5de-be42d7ec57d1”, “stackTrace”: []}
Fri Mar 24 15:49:48 UTC 2023 : Lambda execution failed with status 200 due to customer function error: Unable to import module ‘postHandler’: No module named ‘psycopg2._psycopg’
1. mkdir myfolder and cd myfolder
2. pip install aws-psycopg2 -t .
3. zip -r psycopg2.zip ./*
4. Created layer, uploaded psycopg2.zip bundle and add this layer into my lambda but still getting `No module named ‘psycopg2’`
1. mkdir myfolder and cd myfolder
2. pip install aws-psycopg2 -t .
3. zip -r psycopg2.zip ./*
4. Created layer, uploaded psycopg2.zip bundle and add this layer into my lambda but still getting `No module named ‘psycopg2’`
Can you try bundling this package with your program instead of layers. This is just for debugging. I have followed the steps mentioned in the document and it worked.