Python psycopg2: AttributeError: 'module' 对象没有属性 'extras'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30940167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:11:55  来源:igfitidea点击:

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

pythonpsycopg2importerror

提问by n1000

In my code I use the DictCursorfrom psycopg2.extraslike this

在我的代码中,我像这样使用DictCursorfrompsycopg2.extras

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.

也许我的安装中有些东西是愚蠢的,但我不知道从哪里开始寻找。我用 pip 进行了一些更新,但据我所知没有psycopg2.

采纳答案by All Workers Are Essential

You need to explicitly import psycopg2.extras:

您需要明确导入psycopg2.extras

import psycopg2.extras

回答by Bart?omiej Sza?ach

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

截至 2018 年 7 月,这import psycopg2.extras对我不起作用。以下对我有用:

pip install psycopg2-binary

pip install psycopg2-binary

and later:

然后:

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