Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Python 3.11 > aws-psycopg2 >> "Unable to import module 'lambda_function': No module named 'psycopg2._psycopg'" #392

Closed
panos4tw opened this issue Feb 6, 2024 · 10 comments · Fixed by #454
Assignees

Comments

@panos4tw
Copy link

panos4tw commented Feb 6, 2024

Describe the bug
I import the module as "import psycopg2" and then I tried to use it as "psycopg2.connect(conn_string)" with no success

Screenshots
image

image

image

Layer Version ARN:
arn:aws:lambda:eu-central-1:770693421928:layer:Klayers-p311-aws-psycopg2:1

Framework:
Console

Additional context

@keithrozario
Copy link
Owner

Can you share your full code? Is there a lambda_function.lambda_handler method somewhere here?

@panos4tw
Copy link
Author

Code looks like this, it's the default AWS Lambda Python boilerplate. I ended up making a layer of my own to make this work:

image

@keithrozario
Copy link
Owner

Sorry to hear that, will look into this later this week.

@iliaaz
Copy link
Contributor

iliaaz commented Mar 19, 2024

Hi there! Just ran into the same issue - anything I can do to help debug?

@linusliu
Copy link

linusliu commented May 17, 2024

I also run into the same issue.
Add the following layer to a lambda function, python runtime version 3.11; architecture x86_64
arn:aws:lambda:us-east-1:770693421928:layer:Klayers-p311-aws-psycopg2:1

Will return error:
Response
{
"errorMessage": "Unable to import module 'lambda_function': No module named 'psycopg2._psycopg'",
"errorType": "Runtime.ImportModuleError",
"requestId": "a7956df2-4a44-40c0-a2ea-0ae0e7ca33b7",
"stackTrace": []
}

@austinulfers
Copy link

Getting the same issue with 3.12

@keithrozario
Copy link
Owner

keithrozario commented Nov 10, 2024

HI Folks, can you try the latest layer. It 'hopefully' resolves the issue.

arn:aws:lambda:us-east-2:770693421928:layer:Klayers-p311-psycopg:10

Replace the region with the one you're using.

@austinulfers
Copy link

arn:aws:lambda:us-east-2:770693421928:layer:Klayers-p311-psycopg:10

Can confirm that I was able to get this to work on us-west-2.

@keithrozario
Copy link
Owner

Yay! Thanks @austinulfers

The fix was to pip install psycopg[binary,pool] which installs the binary as well. The previous installation only installed the library but not the libpq.so binary.

Thankfully this fixes it -- but I'll need to figure out how to incorporate this build with the optional dependencies.

@keithrozario
Copy link
Owner

Closing. Please re-open if this doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants