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

Question:

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

Asked By: Jaskaran Singh

||

Answers:

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.

Answered By: Tanuj

add this lib

pip install aws-psycopg2
Answered By: Jaskaran Singh

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

Answered By: Sam

did you get the answer, doesn’t work in my side either.

Answered By: Anna