How to install psycopg2 with "pip" on Python?

I'm using virtualenv and I need to install "psycopg2".

I have done the following:

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

And I have the following messages:

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-infopsycopg2.egg-info

writing pip-egg-infopsycopg2.egg-infoPKG-INFO

writing top-level names to pip-egg-infopsycopg2.egg-infotop_level.txt

writing dependency_links to pip-egg-infopsycopg2.egg-infodependency_links.txt

writing manifest file 'pip-egg-infopsycopg2.egg-infoSOURCES.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 SettingsanlopesApplication Datapipp
ip.log

My question, I only need to do this to get the psycopg2 working?

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

I found this post searching for a Linux solution to this problem.

This post by "goshawk" gave me the solution: run sudo apt-get install libpq-dev python-dev if you are on Ubuntu/Debian.

Update

Since more people have found this answer useful and posted their own solutions, here's a list of them:

Debian/Ubuntu

Python 2

sudo apt install libpq-dev python-dev

Python 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

On CentOS, you need the postgres dev packages:

sudo yum install python-devel postgresql-devel

That was the solution on CentOS 6 at least.


在使用Postgres.app版本9.3.2.0 RC2的Mac Mavericks中安装Postgres后,我需要使用以下代码:

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

链接地址: http://www.djcxy.com/p/60840.html

上一篇: 处理psycopg2中的重复字段

下一篇: 如何在Python上使用“pip”安装psycopg2?