$ pip install psycopg2
Collecting psycopg2
Using cached psycopg2-2.9.9.tar.gz(384 kB)
Preparing metadata(setup.py)... error
error:subprocess-exited-with-error
x python setup.py egg info did not run successfully
exit code:1
->[23 lines of output]
running egg info
creating /tmp/pip-pip-egg-info-x4ekbmyo/psycopg2.egg-info
writing /tmp/pip-pip-egg-info-x4ekbmyo/psycopg2.egg-info/PKG-INFO
writing dependency links to /tmp/pip-pip-egg-info-x4ekbmyo/psycopg2.egg-info/dependency links.txt
writing
top-level names to /tmp/pip-pip-egg-info-x4ekbmyo/psycopg2.egg-info/top level.txt
writing
manifest file '/tmp/pip-pip-egg-info-x4ekbmyo/psycopg2.egg-info/sOURcEs.txt'
Error: pg config executable not found
pg_config is required to build psycopg2 from source.
Please add the directory
containing pg config to the $PATH or specify the full
,executable path with the
option:
python setup.py build ext
--pg-config /path/to/pg config build ...
or with the pg config option in 'setup.cfg'
If you prefer to avoid building psycopg2 from source, please install the PyPI
'psycopg2-binary' package instead.
For further information please check the 'doc/src/install.rst' file (also at
<https://www.psycopg.org/docs/install.html>).
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip
error: metadata-generation-failed
error while generating package metadata
错误的原因是因为在尝试从源代码编译安装 psycopg2 时,系统中缺少 PostgreSQL 的开发文件。特别是,错误信息中提到找不到 pg_config 可执行文件,这个文件是编译过程中必需的。
这里有两种解决方案:
- 安装 psycopg2-binary
如果你不需要从源代码编译 psycopg2 并且只是想使用它,推荐安装预编译的二进制包 psycopg2-binary。可以通过以下命令安装:
pip install psycopg2-binary
这个包更容易安装,因为它不需要编译或安装 PostgreSQL 的开发文件。
- 安装 PostgreSQL 开发文件
如果你特别需要或想要从源代码编译 psycopg2,你需要安装 PostgreSQL 的开发文件,包括 pg_config 可执行文件。
对于 Ubuntu/Debian:
sudo apt-get install libpq-dev
对于 CentOS/Fedora:
sudo yum install postgresql-devel
对于 macOS:
brew install postgresql
安装这些文件后,你应该能够从源代码成功编译 psycopg2。如果你决定编译,确保 pg_config 的路径在你的系统环境变量 $PATH 中,或者在编译时指定路径。