pip安装依赖psycopg2
说明
在学习Fastapi的过程中链接, 使用psycopg2库连接postgresql数据库。
在执行pip install psycopg2
时,报错:
(venv) ✘ ~/Documents/workspace/learning_fastapi master ±
(venv) ✘ ~/Documents/workspace/learning_fastapi master ± pip3 install psycopg2-binary
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting psycopg2-binary
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/d7/1c/8d042630c5ff3c3e6d93c992bd7ecf516d577803b96781c6caa649bbf6e5/psycopg2-binary-2.9.3.tar.gz (380 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [23 lines of output]
running egg_info
creating /private/var/folders/1_/_d2zc5n14ybdp1tgyd0pddb80000gn/T/pip-pip-egg-info-mfvu5r5l/psycopg2_binary.egg-info
writing /private/var/folders/1_/_d2zc5n14ybdp1tgyd0pddb80000gn/T/pip-pip-egg-info-mfvu5r5l/psycopg2_binary.egg-info/PKG-INFO
writing dependency_links to /private/var/folders/1_/_d2zc5n14ybdp1tgyd0pddb80000gn/T/pip-pip-egg-info-mfvu5r5l/psycopg2_binary.egg-info/dependency_links.txt
writing top-level names to /private/var/folders/1_/_d2zc5n14ybdp1tgyd0pddb80000gn/T/pip-pip-egg-info-mfvu5r5l/psycopg2_binary.egg-info/top_level.txt
writing manifest file '/private/var/folders/1_/_d2zc5n14ybdp1tgyd0pddb80000gn/T/pip-pip-egg-info-mfvu5r5l/psycopg2_binary.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
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
(venv) ✘ ~/Documents/workspace/learning_fastapi master ±
解决方法
# python v3.9
# RHEL
sudo dnf install postgresql-devel python39-devel
# Ubuntu
sudo apt update
sudo apt install postgresql-dev python39-dev
# Mac
brew install postgresql
参考
https://stackoverflow.com/questions/11618898/pg-config-executable-not-found
https://www.psycopg.org/docs/install.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具