psycopg2: AttributeError: 'module' object has no attribute 'extras'

PythonPsycopg2Importerror

Python Problem Overview


In my code I use the DictCursor from psycopg2.extras like this

dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

However, all of the sudden I get the following error when I load the cursor:

AttributeError: 'module' object has no attribute 'extras'

Maybe something is dorked in my installation but I have no clue where to start looking. I made some updates with pip, but as far as I know no dependencies of psycopg2.

Python Solutions


Solution 1 - Python

You need to explicitly import psycopg2.extras:

import psycopg2.extras

Solution 2 - Python

As of July 2018, the import psycopg2.extras doesn't work for me. The following works for me:

pip install psycopg2-binary

and later:

>>> import psycopg2.errorcodes
>>> psycopg2.errorcodes.UNIQUE_VIOLATION
'23505'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionn1000View Question on Stackoverflow
Solution 1 - PythonUyghur Lives MatterView Answer on Stackoverflow
Solution 2 - PythonBartłomiej SzałachView Answer on Stackoverflow