Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Psycopg2: 'module' object has no attribute 'connect' [duplicate]

I'm trying to connect to a postgres database with psycopg2:

import psycopg2

try:
    conn = psycopg2.connect("dbname='puppetdb' user='puppetdb' host='172.17.0.1' port='5432' password='puppetdb'")
except Exception, e:
    print "I am unable to connect to the database"
    print e

Which returns:

I am unable to connect to the database
'module' object has no attribute 'connect'

I've made sure that psycopg2 is installed with pip install psycopg2 and it seems like this should work according to the documentation.

Am I doing this wrong?

like image 884
Philip Kirkbride Avatar asked May 03 '17 18:05

Philip Kirkbride


2 Answers

For me it was because I was uploading a zip to AWS lambda and wasn't zipping the folder recursively, i.e. zip lambda.zip * instead of zip -r lambda.zip *. So the module folder was empty.

like image 79
maxymoo Avatar answered Oct 17 '22 09:10

maxymoo


This could be a name shadowing issue.

If your file is called psycopg2.py or if you have a psycopg2.py/psycopg2.pyc file in that directory then it will import your script over the actual pyscopg2 module.

If that's the issue then rename your file to something else.

like image 38
HackerShark Avatar answered Oct 17 '22 09:10

HackerShark