Psycopg2在使用pip安装时出现致命错误,无法安装到Python 3上。

4
$ yum install python3 postgresql python-devel libpqxx-devel
Loaded plugins: langpacks, refresh-packagekit
Package python3-3.3.2-8.fc20.x86_64 already installed and latest version
Package postgresql-9.3.2-2.fc20.x86_64 already installed and latest version
Package python-devel-2.7.5-9.fc20.x86_64 already installed and latest version
Package 1:libpqxx-devel-3.2-0.5.fc20.x86_64 already installed and latest version
Nothing to do

我正试图在我的虚拟环境中安装psycopg2,以便连接Django和PostgreSQL数据库。

在Fedora 19和postgresql-9.2中, pg_config的路径为:

/usr/pgsql-9.2/bin

在 Fedora 20 中,此目录不存在。
但是可以在 /usr/bin/pg_config 中找到 pg_config。
尝试安装 psycopg2:
 $ export PATH=$PATH:/usr/bin; pip install psycopg2
Downloading/unpacking psycopg2
  Downloading psycopg2-2.5.2.tar.gz (685kB): 685kB downloaded
  Running setup.py egg_info for package psycopg2

... -compiling output here- ...

gcc -pthread -Wno-unused-result -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.5.2 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x090302 -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/usr/include/python3.3m -I. -I/usr/include -I/usr/include/pgsql/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.3/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 'gcc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /home/flyer/.virtualenvs/p3test/bin/python3 -c "import setuptools;__file__='/home/flyer/.virtualenvs/p3test/build/psycopg2/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-mrsky4-record/install-record.txt --single-version-externally-managed --install-headers /home/flyer/.virtualenvs/p3test/include/site/python3.3 failed with error code 1 in /home/flyer/.virtualenvs/p3test/build/psycopg2
Storing complete log in /home/flyer/.pip/pip.log

我会感激你的帮助。


2
你是否已经安装了 python3-devel - smani
4个回答

8

您缺少Python3包含文件

./psycopg/psycopg.h:30:20: fatal error: Python.h: No such file or directory

您可以在python3-devel软件包中找到它们。


4
对于Ubuntu系统,这将是python3-dev。谢谢您的提问。 - jangeador

3
安装python3-devel软件包以使其正常工作。
sudo dnf install python3-devel

如果可以工作,那就好;如果需要libpq-fe.h文件,则使用以下命令进行安装:
sudo dnf install postgresql-devel

这可能是一个针对 Fedora 的特定答案。它在其他发行版上也适用吗? - Mark Edington
答案针对 Fedora,其他发行版可能有类似的软件包可用,您可以尝试安装它们并检查是否有效。 - Mahendra Yadav

2

Ubuntu 上,我用以下方法解决了这个问题:

sudo apt-get install libpq-dev

1
正如其他答案所指出的那样,这是由于未安装正确的Python“devel”软件包引起的。在CentOS上,Python“devel”rpm软件包的编号带有合格的版本号。例如,截至本文撰写时,当前版本为:

python34-devel 适用于epel存储库

python36u-devel 适用于ius存储库

示例Yum命令(假设已配置EPEL存储库):

sudo yum install python34-devel

来自服务器故障的相关答案:https://serverfault.com/questions/710354/repository-for-python3-devel-on-centos-7


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