Python 如何使用 pg_config 错误安装 psycopg2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35104097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install psycopg2 with pg_config error?
提问by Chris
I've tried to install psycopg2 (PostgreSQL Database adapater) from this site, but when I try to install after I cd into the package and write
我曾尝试从该站点安装 psycopg2(PostgreSQL 数据库适配器),但是当我在 cd 进入包并写入后尝试安装时
python setup.py install
I get the following error:
我收到以下错误:
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'.
I've also tried 'sudo pip install psycopg2' and I got the same message.
我也试过“sudo pip install psycopg2”,我得到了同样的消息。
After reading through the docs, it asks to look at the setup.cfg file (which is below):
阅读完文档后,它要求查看 setup.cfg 文件(如下所示):
[build_ext]
define=
# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
# PSYCOPG_DEBUG can be added to enable verbose debug information
# "pg_config" is required to locate PostgreSQL headers and libraries needed to
# build psycopg2. If pg_config is not in the path or is installed under a
# different name uncomment the following option and set it to the pg_config
# full path.
#pg_config=
# Set to 1 to use Python datetime objects for default date/time representation.
use_pydatetime=1
# If the build system does not find the mx.DateTime headers, try
# uncommenting the following line and setting its value to the right path.
#mx_include_dir=
# For Windows only:
# Set to 1 if the PostgreSQL library was built with OpenSSL.
# Required to link in OpenSSL libraries and dependencies.
have_ssl=0
# Statically link against the postgresql client library.
#static_libpq=1
# Add here eventual extra libraries required to link the module.
#libraries=
However, I'm not sure if I'm suppose to edit this file, since the documentation states the following:
但是,我不确定是否要编辑此文件,因为文档说明了以下内容:
then take a look at the setup.cfg file.
Some of the options available in setup.cfg are also available as command line arguments of the build_ext sub-command. For instance you can specify an alternate pg_config version using:
$ python setup.py build_ext --pg-config /path/to/pg_config build
Use python setup.py build_ext --help to get a list of the options supported.
I've gotten the list of options supported but I'm not sure where to go from there
我已经获得了支持的选项列表,但我不确定从哪里开始
采纳答案by goCards
If you are on Ubuntu try
如果你在 Ubuntu 上试试
sudo apt-get install python-psycopg2
Otherwise you need to find and install the Postgresql client packages for your distribution. psycopg2 installation from source
否则,您需要为您的发行版查找并安装 Postgresql 客户端软件包。从源代码安装 psycopg2
回答by skywalkerytx
For people building postgres
and psycopg2
from source like me, another solution is here:
对于人们建立postgres
和psycopg2
来自我一样源,另一种解决方案是在这里:
sudo su
export PATH=/usr/local/pgsql/bin:$PATH #or path to your pg_config
Now setup.py from psycopg2 could find pg_config correctly.
现在来自 psycopg2 的 setup.py 可以正确找到 pg_config。
python3 setup.py install
or if you just want to use pip3
, pip3 install psycopg2
should work too.
或者,如果您只想使用pip3
,也pip3 install psycopg2
应该可以使用。
回答by F. P
Upgrading pip worked for me: pip install --upgrade pip
升级 pip 对我有用:pip install --upgrade pip
回答by jahmed31
Debian/Ubuntu
Python 2
蟒蛇 2
sudo apt install libpq-dev python-dev
Python 3
蟒蛇 3
sudo apt install libpq-dev python3-dev
Additional
额外的
If none of the above solve your issue, try
sudo apt install build-essential
or
sudo apt install postgresql-server-dev-all
With pip
带点
Install the psycopg2-binary
PyPI package instead, it has Python wheels for Linux and Mac OS.
psycopg2-binary
而是安装PyPI 包,它具有适用于 Linux 和 Mac OS 的 Python 轮子。
pip install psycopg2-binary
回答by Seph Cordovano
I was getting this issue because I hadn't yet installed PostgreSQL on my machine yet. On mac just a simple brew install postgresql
fixed the problem.
我遇到这个问题是因为我还没有在我的机器上安装 PostgreSQL。在 mac 上只是一个简单的brew install postgresql
解决了这个问题。
回答by sp1111
If you need to install without compiling:
如果需要安装而不编译:
pip install psycopg2-binary
https://www.psycopg.org/docs/install.html#binary-install-from-pypi
https://www.psycopg.org/docs/install.html#binary-install-from-pypi
Note:The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements. If you are the maintainer of a publish package depending on psycopg2 you shouldn't use ‘psycopg2-binary' as a module dependency. For production use you are advised to use the source distribution.
注意:psycopg2-binary 包是为初学者准备开始使用 Python 和 PostgreSQL 而无需满足构建要求。如果您是依赖 psycopg2 的发布包的维护者,则不应使用 'psycopg2-binary' 作为模块依赖项。对于生产用途,建议您使用源代码分发。