113

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.

n1000
  • 5,058
  • 10
  • 37
  • 65

2 Answers2

197

You need to explicitly import psycopg2.extras:

import psycopg2.extras
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
  • As of July 2020, the import psycopg2.extras works for me – Amir Samakar Jul 03 '20 at 20:46
  • Note: need `import psycopg2` and `import psycopg2.extras` and use as `psycopg2.extras.DictCursor`, no `extras.DictCursor` – e-info128 Dec 26 '21 at 21:01
  • @e-info128 In order to use `psycopg2.extras.DictCursor`, only `import psycopg2.extras` is needed. The `import psycopg2` isn't strictly needed, though, I personally always include it because it makes the dependency for `psycopg2.connect` obvious. – Uyghur Lives Matter Dec 29 '21 at 17:34
7

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'
Bartłomiej Szałach
  • 2,393
  • 3
  • 30
  • 50