Unable to import module 'lambda_function': No module named 'psycopg2._psycopg aws lambda function

12.4k Views Asked by At

I have installed the psycopg2 with this command in my package folder :

pip install --target ./package psycopg2 # Or
pip install -t ./package psycopg2

now psycopg2 module is in my package and I have created the zip and upload it in AWS lambda.

In my local sprint is working fine but on AWS lambda it was not working.

It shows me error

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'psycopg2._psycopg'",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}

my lambda code is

import psycopg2

def lambda_handler():
   print('hello')

my all other modules are working fine

5

There are 5 best solutions below

1
On BEST ANSWER

add this lib

pip install aws-psycopg2
0
On

I faced the same issue as I created the virtual env for layers on Mac. to solve this, I used the below command.

pip install --platform=manylinux1_x86_64 --only-binary=:all: psycopg2-binary --target "your lambda layer path"

in my case it was:

lambda/layer/python/lib/python3.8/site-packages

You can also use --upgrade if some of the required libs are already there.

1
On

In my case, I had to add psycopg2-binary to the requirements.txt

0
On

I have to zip the whole library it installed on docker you can get the images from https://hub.docker.com/r/mlupin/docker-lambda/tags

I was using python3.11

Make sure you have docker installed.

Step 1. Create the library folder

cd ~ && mkdir python-lib && cd python-lib
echo 'psycopg2-binary' > requirements.txt

Step 2. Make the library using docker image

docker run -v "$PWD":/var/task "mlupin/docker-lambda:python3.11-build" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.11/site-packages/; exit"
zip -r psycopg2.zip python

then you have to upload that zip file to AWS lambda's layer part

0
On
> pip3 install aws-psycopg2 -t .

Collecting aws-psycopg2
  Using cached aws_psycopg2-1.3.8-py3-none-any.whl (73.8 MB)
Installing collected packages: aws-psycopg2
Successfully installed aws-psycopg2-1.3.8