我正在使用virtualenv,我需要安装“psycopg2”。

我做了以下几点:

pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160

我有以下信息:

Downloading/unpacking http://pypi.python.org/packages/source/p/psycopg2/psycopg2
-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
  Downloading psycopg2-2.4.tar.gz (607Kb): 607Kb downloaded
  Running setup.py egg_info for package from http://pypi.python.org/packages/sou
rce/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
    Error: pg_config executable not found.

    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'.
    Complete output from command python setup.py egg_info:
    running egg_info

creating pip-egg-info\psycopg2.egg-info

writing pip-egg-info\psycopg2.egg-info\PKG-INFO

writing top-level names to pip-egg-info\psycopg2.egg-info\top_level.txt

writing dependency_links to pip-egg-info\psycopg2.egg-info\dependency_links.txt

writing manifest file 'pip-egg-info\psycopg2.egg-info\SOURCES.txt'

warning: manifest_maker: standard file '-c' not found

Error: pg_config executable not found.



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'.

----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in C:\Documents and Settings\anlopes\Application Data\pip\p
ip.log

我的问题是,我只需要这样做来让psycopg2工作?

python setup.py build_ext --pg-config /path/to/pg_config build ...

当前回答

如果PIP不工作,您可以从这里下载.whl文件https://pypi.python.org/pypi/psycopg2 提取它. . 而不是python setup.py install

其他回答

这对我来说是有效的(关于RHEL, CentOS:

sudo yum install postgresql postgresql-devel python-devel

现在在你的pip install中包含你的postgresql二进制目录的路径:

sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2

确保包含正确的路径。就这些。

更新:对于python3,请安装python3-devel而不是python-devel

注意:自从一段时间以前,在PyPI中有Windows的二进制轮,所以这对Windows用户来说应该不再是一个问题。下面是Linux和Mac用户的解决方案,因为他们中的许多人是通过网络搜索找到这篇文章的。


选项1

安装psycopg2-binary PyPI包,它有适用于Linux和Mac OS的Python轮。

pip install psycopg2-binary

选项2

安装从源代码构建psycopg2包的先决条件:

Debian/Ubuntu

Python 3

sudo apt install libpq-dev python3-dev

你可能需要安装python3.8-dev或类似的程序,例如Python 3.8。

Python 2

sudo apt install libpq-dev python-dev

如果这还不够,那就试试

sudo apt install build-essential

or

sudo apt install postgresql-server-dev-all

在再次安装psycopg2之前。

CentOS 6

请看班杰的回答

操作系统

请看nichochar的回答

MacOS系统安装

Following are the steps, which worked for me and my team members while installing psycopg2 on Mac OS Big Sur and which we have extensively tested for Big Sur. Before starting make sure you have the Xcode command-line tool installed. If not, then install it from the Apple Developer site. The below steps assume you have homebrew installed. If you have not installed homebrew then install it. Last but not the least, it also assumes you already have PostgreSQL installed in your system, if not then install it. Different people have different preferences but the default installation method on the official PostgreSQL site via Enterprise DB installer is the best method for the majority of people.

Put up the linkage to pg_config file in your .zshrc file by: export PATH="$PATH:/Library/PostgreSQL/12/bin:$PATH". This way you are having linkage with the pg_config file in the /Library/PostgreSQL/12/bin folder. So if your PostgreSQL installation is via other means, like Postgres.app or Postgres installation via homebrew, then you need to have in your .zshrc file the link to pg_config file from the bin folder of that PostgreSQL installation as psycopg2 relies on that. Install OpenSSL via Homebrew using the command brew install openssl. The reason for this is that libpq, the library which is the basis of psycopg2, uses openssl - psycopg2 doesn't use it directly. After installing put the following commands in your .zshrc file: export PATH="/usr/local/opt/openssl@1.1/bin:$PATH" export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib" export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include" By doing this you are creating necessary linkages in your directory. These commands are suggested by brew while you install openssl and have been directly picked up from there. Now comes the most important step, which is to install libpq using the command brew install libpq. This installs libpq library. As per the documentation

libpq是C应用程序程序员到PostgreSQL的接口。libpq是一组库函数,允许客户端程序将查询传递给PostgreSQL后端服务器,并接收这些查询的结果。

使用brew Link libpq链接libpq,如果这不起作用,那么使用命令:brew Link libpq——force。 还要在.zshrc文件中放入以下export PATH="/usr/local/opt/libpq/bin:$PATH"。这将为libpq库创建所有必要的链接。 现在重新启动终端或使用以下命令源~/.zshrc。 即使在conda环境中工作,这也可以工作。 注意:pip install psycopg2-binary应该避免,因为根据psycopg2库的开发人员的说法

不鼓励在生产环境中使用-binary包,因为过去它们在多线程环境中被证明是不可靠的。这可能已经在最近的版本中修复,但我从来没有设法重现失败。

在Ubuntu上,我只需要postgres开发包:

sudo apt-get install postgresql-server-dev-all

*在virtualenv中测试

对于fedora用户:

Libpq-dev是依赖项之一,但是,在安装中应该这样命名这个包:libpq-devel

所以这应该解决了Fedora的问题:

sudo yum install libpq-devel