安装psycopg2遇python setup.py egg_info执行失败,求解决方案
解决psycopg2安装时
pg_config executable not found错误 问题重现
安装psycopg2==2.8.6时触发以下错误:
Collecting psycopg2==2.8.6 (from -r requirements.txt (line 45)) Using cached psycopg2-2.8.6.tar.gz (383 kB) Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [37 lines of output] ... 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: ...
已升级setuptools,但问题未解决。
解决方案
方案1:安装PostgreSQL开发依赖包
pg_config是PostgreSQL官方工具,需先安装对应系统的开发包:
- Debian/Ubuntu 系统:
sudo apt-get install libpq-dev - RHEL/CentOS 系统:
sudo yum install postgresql-devel - macOS(使用Homebrew):
brew install postgresql
安装完成后重新执行pip install psycopg2==2.8.6即可。
方案2:安装预编译的psycopg2-binary包
不想编译源码的话,直接用官方预编译版本:
pip install psycopg2-binary==2.8.6
该包无需依赖系统PostgreSQL开发环境,适合快速解决问题。
方案3:手动指定pg_config路径(已装PostgreSQL但路径未配置)
先找到pg_config的实际路径:
which pg_config
假设路径为/usr/pgsql-12/bin/pg_config,执行以下命令安装:
pip install psycopg2==2.8.6 --global-option=build_ext --global-option="--pg-config=/usr/pgsql-12/bin/pg_config"
内容的提问来源于stack exchange,提问作者lungsang
相关产品推荐
相关产品推荐

