Imran
Imran

Reputation: 149

psycopg2 : module 'psycopg2' has no attribute 'connect'

I'm trying to connect to PostgreSQL in Python using psycopg2, and I'm passing my connection parameters to the psycopg2.connect() function as follows:

session = psycopg2.connect(
    host="hostvalue", database="db_value", user="username", password="password"
)

But this is throwing the following error:

module 'psycopg2' has no attribute 'connect'

I've already installed psycopg2-binary on my mac, as is evident when I try installing it with pip again:

$ pip3 install psycopg2-binary
Requirement already satisfied: psycopg2-binary in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (2.8.4)

I really don't know why I'm seeing this error. Could someone please help me out here.

Upvotes: 0

Views: 6411

Answers (1)

Dewald Abrie
Dewald Abrie

Reputation: 1472

Can you try calling dir on the module and print the output like so:

import psycopg2, pprint
pprint.pprint(dir(psycopg2))
['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__',
 '__warningregistry__',
 '_connect',
 '_ext',
 '_json',
 '_psycopg',
 '_range',
 'apilevel',
 'connect',
 'extensions',
 'paramstyle',
 'threadsafety',
 'tz',
 'warn']

You should see the 'connect' method in the list as shown above.

Upvotes: 1

Related Questions