jkehler / awslambda-psycopg2

1.1k stars 360 forks source link

module 'psycopg2' has no attribute 'connect' #38

Closed irinacheng closed 4 years ago

irinacheng commented 6 years ago

When I was trying to make a connection, it gave me error that "module 'psycopg2' has no attribute 'connect'

I followed the instruction, and rename the package to psycopg2.

Here is the sample code:

try:
    conn = psycopg2.connect(connection_string )
except Exception as e:
    print('Error {}'.format(str(e)))
seth10 commented 5 years ago

Same issue here. I followed @jkehler's instructions in the README.md and my resulting directory looks the same as psycopg2-3.6 (but of course mine is named "psycopg2"), and when I try using it in a Lambda I get the same "module 'psycopg2' has no attribute 'connect'".

print(dir(psycopg2)) yields ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']. On the EC2 instance I built it on though, that yields ['BINARY', 'Binary', 'DATETIME', 'DataError', 'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'IntegrityError', 'InterfaceError', 'InternalError', 'NUMBER', 'NotSupportedError', 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'Time', 'TimeFromTicks', 'Timestamp', 'TimestampFromTicks', 'Warning', '__builtins__', '__cached__', '__doc__', '__file__', '__libpq_version__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_connect', '_ext', '_json', '_psycopg', '_range', 'apilevel', 'connect', 'extensions', 'paramstyle', 'threadsafety', 'tz']

akshatkish commented 5 years ago

How did you finally solve the issue given a lot of time has gone by now?

seth10 commented 5 years ago

I'm not sure about @irinacheng but I wasn't able to solve it, I ended up using a different database instead of PostgreSQL.

irinacheng commented 5 years ago

@akshatkish I did not solve this issue either. I ended up using other database as well.

akshatkish commented 5 years ago

Okay ,so I solved this issue in my own way and it can be said to be a quick fix but I am putting it out here so that in case anyone in the future encounters the same problem, they are atleast able to get around the problem for the time being. So, when I was constantly getting 'module 'psycopg2' has no attribute 'connect' ', I was using Python 3.6 with the psycopg2-3.6 directory renamed as psycopg2. So, I switched my langauage to Python 2.7 and uploaded the psycopg2 with lambda function zip file, instead. This made me write my lambda function in Python 2.7 and get around the actual issue, but it works.

sinramyeon commented 5 years ago

There were several related problems with applications module liike #14 ....

psyhomb commented 5 years ago

Anyone used aws-psycopg2 project? You can build the latest version of this module yourself, with ease.

Or you can use already prebuilt modules for python2.7 and python3.7 https://gitlab.com/psyhomb/aws-psycopg2#prebuilt-modules

iamadouhassane commented 5 years ago

Hello @psyhomb i hope you are fine ? Please can you help me to use psycopg2 on my lambda fonction with python3.6 ; I am new in aws and python. In hope to read read quickly , Thanks

cj-mclaughlin commented 5 years ago

Hello @psyhomb i hope you are fine ? Please can you help me to use psycopg2 on my lambda fonction with python3.6 ; I am new in aws and python. In hope to read read quickly , Thanks

@iamadouhassane I couldn't get any of the 3.6 modules working myself, try to download and use the python3.7 module @psyhomb linked here: https://gitlab.com/psyhomb/aws-psycopg2#prebuilt-modules Simply rename the py3.7 folder to psycopg2 and then zip it along with your code and it should work on lambda.

iamadouhassane commented 5 years ago

Hello @psyhomb  thanks you for your  answer . I resolt the problem by installing psycopg2 -binary.

Thanks you lot,Ismael Le mardi 18 juin 2019 à 18:35:26 UTC+2, Connor McLaughlin notifications@github.com a écrit :

Hello @psyhomb i hope you are fine ? Please can you help me to use psycopg2 on my lambda fonction with python3.6 ; I am new in aws and python. In hope to read read quickly , Thanks

@iamadouhassane I couldn't get any of the 3.6 modules working myself, try to download and use the python3.7 module @psyhomb linked here: https://gitlab.com/psyhomb/aws-psycopg2#prebuilt-modules Simply rename the py3.7 folder to psycopg2 and then zip it along with your code and it should work on lambda.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

psyhomb commented 5 years ago

@iamadouhassane you've probably missed the warning, psycopg2-binary should not be used in production environment, that's why I offered an easy solution for building psycopg2 from source.

See the warning at end of page:

The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources.

glmourad commented 5 years ago

Hello @psyhomb i hope you are fine ? Please can you help me to use psycopg2 on my lambda fonction with python3.6 ; I am new in aws and python. In hope to read read quickly , Thanks

@iamadouhassane I couldn't get any of the 3.6 modules working myself, try to download and use the python3.7 module @psyhomb linked here: https://gitlab.com/psyhomb/aws-psycopg2#prebuilt-modules Simply rename the py3.7 folder to psycopg2 and then zip it along with your code and it should work on lambda.

Worked for you?

iamadouhassane commented 5 years ago

Hello  @psyhomb  Thanks for your answer . It worke finally. someone help me. Thanks, Ismael Le vendredi 5 juillet 2019 à 12:19:30 UTC+2, glmourad notifications@github.com a écrit :

Hello @psyhomb i hope you are fine ? Please can you help me to use psycopg2 on my lambda fonction with python3.6 ; I am new in aws and python. In hope to read read quickly , Thanks

@iamadouhassane I couldn't get any of the 3.6 modules working myself, try to download and use the python3.7 module @psyhomb linked here: https://gitlab.com/psyhomb/aws-psycopg2#prebuilt-modules Simply rename the py3.7 folder to psycopg2 and then zip it along with your code and it should work on lambda.

Worked for you?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

imranshaikmuma commented 4 years ago

just use this layer works well: https://github.com/jetbridge/psycopg2-lambda-layer

gibsonbailey commented 1 year ago

I experienced this error message when I forgot to zip the psycopg2 directory before zipping with lambda_function.py as the docs specify.