如何安装pg_config错误的psycopg2?

Chr*_*ris 11 python postgresql psycopg2

我已经尝试从此站点安装psycopg2(PostgreSQL数据库adapater),但是当我尝试将CD装入软件包并写入后进行安装时

python setup.py install 
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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'.
Run Code Online (Sandbox Code Playgroud)

我也尝试过“ sudo pip install psycopg2”,并且得到了相同的消息。

阅读完文档后,它要求查看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=
Run Code Online (Sandbox Code Playgroud)

但是,我不确定是否要编辑此文件,因为文档中指出以下内容:

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.
Run Code Online (Sandbox Code Playgroud)

我已经获得了支持的选项列表,但是我不确定从那里去哪里

Sep*_*ano 23

我遇到这个问题是因为我还没有在我的机器上安装 PostgreSQL。在 mac 上只是一个简单的brew install postgresql解决了这个问题。


jah*_*d31 22

Debian/Ubuntu
Run Code Online (Sandbox Code Playgroud)

Python 2

sudo apt install libpq-dev python-dev
Run Code Online (Sandbox Code Playgroud)

Python 3

sudo apt install libpq-dev python3-dev
Run Code Online (Sandbox Code Playgroud)

额外

If none of the above solve your issue, try

sudo apt install build-essential
or

sudo apt install postgresql-server-dev-all
Run Code Online (Sandbox Code Playgroud)

  • 我也有这个问题。可悲的是,使用我的软件包apt安装`python3-dev`将无法工作。我所做的是对该答案的排列:1.`sudo pip install --upgrade pip` 2.`sudo apt install -y libpq-dev postgresql-server-dev-all` 3.`sudo pip3 install psycopg2`如果所有`sudo`是必需的,但是我不在乎,所以我把它们放了。 (2认同)

小智 7

如果您需要安装而不进行编译:

pip install psycopg2-binary
Run Code Online (Sandbox Code Playgroud)

  • 来自 psycopg2 的文档 (https://www.psycopg.org/docs/install.html#binary-install-from-pypi): 注意 psycopg2-binary 包旨在让初学者开始使用 Python 和 PostgreSQL,而无需使用需要满足构建要求。如果您是依赖于 psycopg2 的发布包的维护者,则不应使用“psycopg2-binary”作为模块依赖项。对于生产用途,建议您使用源发行版。请将此添加到您的答案中。 (4认同)

goC*_*rds 5

如果您使用的是Ubuntu,请尝试

sudo apt-get install python-psycopg2
Run Code Online (Sandbox Code Playgroud)

否则,您需要找到并安装用于发行版的Postgresql客户端软件包。从源代码安装psycopg2


Pun*_*rud 5

在配备 M1 的 Macbook 上我必须安装 postgresql

brew install postgresql
Run Code Online (Sandbox Code Playgroud)

(如果你没有brew:https://brew.sh

然后再次运行安装

python3 -m pip install psycopg2-binary
Run Code Online (Sandbox Code Playgroud)