Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 987
Jaskaran Singh Avatar asked May 29 '26 21:05

Jaskaran Singh


2 Answers

add this lib

pip install aws-psycopg2
like image 155
Jaskaran Singh Avatar answered May 31 '26 10:05

Jaskaran Singh


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.

like image 31
Tanuj Avatar answered May 31 '26 10:05

Tanuj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!