Psycopg2: AttributeError: “模块”对象没有“额外”属性

在我的代码中,我像这样使用来自 psycopg2.extrasDictCursor

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

但是,当我加载光标时,突然出现以下错误:

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

也许有些东西在我的装置里呆了,但我不知道从哪里开始找。我用 pip 做了一些更新,但据我所知没有依赖于 psycopg2

40762 次浏览

You need to explicitly import psycopg2.extras:

import psycopg2.extras

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'