我正在使用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 ...

当前回答

在windows XP上,如果postgres没有安装,你会得到这个错误…

其他回答

我最近在一台windows计算机上配置了psycopg2。最简单的安装是使用windows可执行二进制文件。你可以在http://stickpeople.com/projects/python/win-psycopg/上找到它。

要在虚拟环境中安装本机二进制文件,请使用easy_install:

C:\virtualenv\Scripts\> activate.bat
(virtualenv) C:\virtualenv\Scripts\> easy_install psycopg2-2.5.win32-py2.7-pg9.2.4-release.exe

在CentOS上,你需要postgres开发包:

sudo yum install python-devel postgresql-devel

这至少是CentOS 6上的解决方案。

对于fedora用户:

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

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

sudo yum install libpq-devel

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包,因为过去它们在多线程环境中被证明是不可靠的。这可能已经在最近的版本中修复,但我从来没有设法重现失败。

在Mac Mavericks与Postgres。在安装Postgres后,我需要使用以下代码:

sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin pip install psycopg2 . sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin