jkehler / awslambda-psycopg2

1.12k stars 363 forks source link

ModuleNotFoundError: No module named 'psycopg2._psycopg' #51

Closed soft2dev closed 2 years ago

soft2dev commented 5 years ago

When I deploy it, I get this error.

I checked _psycopg module but there is no this module. I used psycopg2-3.7

I hope your help.

salerak commented 5 years ago

how are you building and deploying this? because you'll need to copy the right folder depending on the version for example, I'm using the ssl version 3.7 and I'm doing this mv awslambda-psycopg2/with_ssl_support/psycopg2-3.7/ ./v-env/lib/python3.7/site-packages/psycopg2 . Make sure you're also using python version 3.7

jayshah96 commented 5 years ago

facing the same issue. I tried without SSL for python 3.7.

gffield commented 5 years ago

Getting this issue as well with psycopg2-3.6

mv ~/Downloads/awslambda-psycopg2-master/with_ssl_support/psycopg2-3.6 psycopg2

genii90 commented 4 years ago

On a windows 10 machine. I am getting the same error. I copied psycopg2-3.7 to my root directory and renamed the folder to psycopg2. I package and deploy my application using "sam" commands. My Lambda environment is set to Python 3.7. Are there any special steps when using a Windows environment?

genii90 commented 4 years ago

I ended up using a different library: psycopg2-binary in my requirement.txt file and it working fine now.

evohnave commented 4 years ago

Has anyone been able to get the 3.7 files to work with Lambda? I got 3.6 to work (make sure your files are in ./python/psycopg2 and zip the python directory before creating your layer) but couldn't get 3.7 to work at all and get the psycopg2._psycopg missing error.

shlomi-viz commented 4 years ago

any updates on getting it to work with Python 3.7?

shlomi-viz commented 4 years ago

I solved it by using psycopg2-binary==2.8.3

Marco-Rod commented 4 years ago

I solved it by using psycopg2-binary==2.8.3

which python version?

shlomi-viz commented 4 years ago

I solved it by using psycopg2-binary==2.8.3

which python version?

Python3.7

matthewhegarty commented 4 years ago

I couldn't get it to work with a requirements.txt file.

The only thing that worked for me was to statically include dependencies in the same directory as the handler (I used the psycopg2-3.6 lib), but 3.7 works as well:

my_handler.py
-- boto3
-- psycopg2

Simple Makefile (include the static libraries in a static_deps dir):

FUNCTION = my-function

all: build

.PHONY: clean build

clean:
    rm -rf build

build: clean
    mkdir build
    zip -r build/$(FUNCTION).zip . -x "build*" "Makefile"  "static_deps*" \
        ".gitignore" "*__pycache__*" 
    cp -r static_deps/boto3 static_deps/psycopg2 build; \
    cd build; zip -g -r ./$(FUNCTION).zip . -x "*__pycache__*"
sinjihn-4th-valley commented 4 years ago

im having this issue just trying to run locally. Python = 3.7.6. I copied the python 3.7 folder.

>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sinjihnsmith/Documents/Dev/GitHub/repositories/swan/psycopg2/__init__.py", line 50, in <module>
    from psycopg2._psycopg import (                     # noqa
ModuleNotFoundError: No module named 'psycopg2._psycopg'
>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sinjihnsmith/Documents/Dev/GitHub/repositories/swan/psycopg2/__init__.py, line 50, in <module>
    from psycopg2._psycopg import (                     # noqa
ModuleNotFoundError: No module named 'psycopg2._psycopg'
Jugbot commented 4 years ago

Was this issue solved?

genii90 commented 4 years ago

I didn't solve it. I found a work around by using psycopg2-binary instead. Not sure that solution is sufficient for you.

NedyalkoKr commented 4 years ago

On Windows 10 downgrading to psycopg2-binary==2.8.5 with python 3.8.6 works and django. I don't know why 2.8.6 doesn't work.

fluxquantum commented 4 years ago

I solved it by using psycopg2-binary==2.8.3

Was this from doing a pip install as part of deployment or using lambda layers? I ideally want to y. use that version of psycopg2-binary, but just settled with using psycopg2 offered in this repo for now.

fluxquantum commented 4 years ago

Has anyone been able to get the 3.7 files to work with Lambda? I got 3.6 to work (make sure your files are in ./python/psycopg2 and zip the python directory before creating your layer) but couldn't get 3.7 to work at all and get the psycopg2._psycopg missing error.

To create a layer - I had to create a folder of this path: /python/lib/python3.7/site-packages/psycopg2

shlomi-viz commented 4 years ago

I use it as a regular package and install it with pip.

danielkuske commented 3 years ago

I was facing the same error after I moved the psycopg2 folder from the root level of my project to a subfolder (package/psycopg2). It seems, that several import statements inside psycopg2 do not work after that. My intention was to have a cleaner structure. I finally achieved that by uploading psycopg2 to a lambda layer (which makes the lambda function even cleaner).

If you want to do so, just put the psycopg2 folder into a folder called python, zip it and upload it to a new lambda layer. This lambda layer can now be used by every lambda function that needs psycopg2.

Hope this helps someone! :)

worksofindustry commented 2 years ago

I was able to get around the issue by creating a layer in Lambda, which is the preferred method for code reuse. Make sure the layer and function have the same versions of Python. At the lambda's container runtime, the packaged .zip file is extracted into the directory '/opt/' Then adding in below to your lambda_function.py you should be able to add the module `import sys

sys.path.append("/opt/")

import psycopg2`

LooRDAj commented 1 year ago

i got help from here