安装Python的Postgres出现错误(psycopg2)

97

我尝试在我的环境中安装psycopg2,但是出现以下错误:

(venv)avlahop@apostolos-laptop:~/development/django/rhombus-dental$ sudo pip install psycopg2
Downloading/unpacking psycopg2,
Downloading psycopg2-2.5.2.tar.gz (685kB): 685kB downloaded
Running setup.py egg_info for package psycopg2

Installing collected packages: psycopg2
  Running setup.py install for psycopg2
    building 'psycopg2._psycopg' extension
    x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall Wstrict-prototypes -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.5.2 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x09010D -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/usr/include/python2.7 -I. -I/usr/include/postgresql -I/usr/include/postgresql/9.1/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-2.7/psycopg/psycopgmodule.o -Wdeclaration-after-statement
    In file included from psycopg/psycopgmodule.c:27:0:
    ./psycopg/psycopg.h:30:20: fatal error: Python.h: Δεν υπάρχει τέτοιο αρχείο ή κατάλογος
     #include <Python.h>
                    ^
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    Complete output from command /usr/bin/python -c "import setuptools;__file__='/tmp/pip_build_root/psycopg2/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-SgfQCA-record/install-record.txt --single-version-externally-managed:
running install

running build

running build_py

creating build

creating build/lib.linux-x86_64-2.7

creating build/lib.linux-x86_64-2.7/psycopg2

copying lib/pool.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/errorcodes.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/__init__.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/_json.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/_range.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/extensions.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/psycopg1.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/tz.py -> build/lib.linux-x86_64-2.7/psycopg2

copying lib/extras.py -> build/lib.linux-x86_64-2.7/psycopg2

creating build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/testconfig.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copyng tests/test_bug_gc.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_dates.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_copy.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_cancel.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_bugX000.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_extras_dictcursor.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_psycopg2_dbapi20.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_types_basic.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_async.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_lobject.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_cursor.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_with.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/__init__.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_types_extras.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/testutils.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_notify.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_green.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_quote.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_connection.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_transaction.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/dbapi20.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/test_module.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

copying tests/dbapi20_tpc.py -> build/lib.linux-x86_64-2.7/psycopg2/tests

running build_ext

building 'psycopg2._psycopg' extension

creating build/temp.linux-x86_64-2.7

creating build/temp.linux-x86_64-2.7/psycopg

x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.5.2 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x09010D -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/usr/include/python2.7 -I. -I/usr/include/postgresql -I/usr/include/postgresql/9.1/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-2.7/psycopg/psycopgmodule.o -Wdeclaration-after-statement

In file included from psycopg/psycopgmodule.c:27:0:

./psycopg/psycopg.h:30:20: fatal error: Python.h: No such file or directory
 #include <Python.h>

                ^

compilation terminated.

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools;__file__='/tmp/pip_build_root/psycopg2/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-SgfQCA-record/install-record.txt --single-version-externally-managed failed with error code 1 in /tmp/pip_build_root/psycopg2
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 148, in main
    return command.main(args[1:], options)
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 169, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 54: ordinal not in range(128)

我试图安装gunicorn时也遇到了错误。我按照这个示例进行操作,但没有成功。我的虚拟环境是使用--no-site-packages标志创建的。我使用的是Ubuntu 13.10和希腊语环境。也许是这个原因导致的?


12
您是否安装了python-dev软件包?如果您使用的是基于Debian的发行版(例如Ubuntu),请尝试apt-get install python-dev,然后再次尝试安装psycopg2。这是为了编译用C或C++编写的Python扩展所需的。 - lanzz
1
谢谢...它起作用了!!! - Apostolos
3
值得注意的是,如果你使用的是非Debian发行版,你需要使用 yum install python-devel。这种差异的原因让我感到困惑,只会带来混乱和麻烦。 - Richard D
好问题,应该点赞。 - Timo
1
@RichardD 我该如何使用yum安装Python3的python-devel? - Joshua Gomez
12个回答

186

如果您想要编译使用C或C ++编写的Python扩展,例如psycopg2,则需要安装python-dev包。如果您正在运行基于Debian的发行版(例如Ubuntu),则可以通过运行以下命令来安装python-dev:

sudo apt install python-dev
或者
sudo apt install python3-dev

根据你的 Python 版本进行安装(例如:sudo apt install python3.8-dev)。

然后,按照通常的方式在你的virtualenv环境中安装 psycopg2


16
如果你正在使用Python 3,这可能不会(或者很可能不会)起作用,你需要执行apt-get install python3-dev代替。 - Paul Whalen
18
为了安装 Python 3.5,我需要执行命令 apt-get install python3.5-dev - Or Duan
8
同样适用于 python3.7-dev - Nik
6
同样适用于python3.8-dev - marou
5
我认为这里可能存在一种模式... - lanzz
显示剩余2条评论

60

对于 Ubuntu 14.04,使用 Docker 镜像 python:3.4.3-slim,以下组合方式适用于我:

sudo apt-get update
sudo apt-get install -y build-essential
sudo apt-get install -y python3.4-dev
sudo apt-get install -y libpq-dev

pip3 install psycopg2

请注意 build-essential 软件包。这在我的情况下非常重要。


4
"build essential 对我也起了作用。我使用的是 Ubuntu 16.04 和 Python 2.7.12。" - wilblack
2
一样!build-essential是缺失的部分。 - ankush981
Ubuntu 16.04,Python 3.5,对我来说一样——缺少了build-essential。 - Max Malysh
“build-essential” 也是我在 WSL Ubuntu 20.04 LTS 中缺失的关键组件。 - unlimitedfox
这个答案在两年前对我有用,今天却没用了,哈哈。 - NMukama

15

我尝试了所有的解决方案,但只有一种方法有效。

pip install psycopg2-binary

安装库后,您可以在Python文件中使用import psycopg2,然后进一步进行数据库连接配置。

13

在我的情况下(安装有Python 2.7和Python 3.4的Ubuntu 12.04)

sudo apt-get install python3-dev 

不够。关键是什么起了作用。

sudo apt-get install python3.4-dev

太好了!这也是我的问题。 - Vitalii Ponomar
在我的情况下,apt install python3.8-dev 挽救了我。 - weaming

6

在Amazon Linux中使用 Python3虚拟环境,我需要执行以下操作:

sudo yum install python3-devel postgresql-devel
pip install psycopg2

4

为了使用最新的Python 3.7,我需要安装python3.7-dev

sudo apt-get install -y python3.7-dev


4

Debian:

sudo apt-get install gcc

为什么这个被踩了?这个解决了我的问题。在Ubuntu 19中,gcc不是默认预安装的。 - Saturnix
这个救了我们的命,尝试在Ubuntu EC2服务器上安装psycopg2。谢谢! - Risco Hardy

3

首先安装python-dev。

sudo apt-get install python-dev

如果要正确使用,请尝试以下方法:

这与您的终端编码设置不为UTF-8有关。以下是我的终端示例:

$ echo $LANG
es_UY.UTF-8
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
>>> s1 = s.decode('utf-8')
>>> print s1
(。・ω・。)ノ
>>> 

如果我取消设置,我会得到与您相同的错误:
$ unset LANG
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
>>> s1 = s.decode('utf-8')
>>> print s1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)
>>> 

1
Unicode错误只是与可视化相关的问题。实际的致命错误是无法找到“Python.h”头文件。 - lanzz
1
谢谢,我也得安装服务器开发包:sudo apt-get install postgresql-server-dev-9.1,如果使用的是PostgreSQL 9.3,则要安装9.3版本的开发包。 - radtek

2

我使用的是Linux的Ubuntu发行版,并进行了以下操作:

安装最新版本的python3-dev库,以使C扩展适用于python。第一条命令更新Ubuntu中的软件包列表,以便安装最新版本的python3-dev。

sudo apt-get update

sudo apt-get install python3-dev

然后,我需要安装PostgreSQL的缺失开发库:"libpq-dev"包。

sudo apt-get install libpq-dev

这之后,我就能够完成我依赖项的安装了。


1

尝试了一切方法都无果之后,使用sudo apt-get install -y build-essential帮助我解决了问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接