Install psycopg2 for Anaconda Python

Question:

I have Anaconda Python 3.4, however whenever I run older code I switch to Anaconda Python 2.7 by typing “source activate python2”. My issue is that I have psycopg2 installed for Anaconda Python 3.4, but not for Anaconda Python 2.7. When I run pip install psycopg2 (on Python 2.7) I get the following message:

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

I am fairly new to programming and need help in:

1. Obtaining directory containing pg_config
2. Finding the path to Anaconda Python 2.7 
3. Adding pg_config to the PATH.

After I complete these steps I should be able to pip install Install psycopg2

Asked By: user3062459

||

Answers:

You need development system package for PostgreSQL which contains header files required to compile psycopg2 extension. For my CentOS 64 bit the command to install is:

yum install postgresql-devel.x86_64

but it depends on the OS – for Ubuntu that would be apt-get install ... – the name of the package varies slightly between distros.
Steps 2 and 3 should be unnecessary after you do this.

EDIT: For Mac OS that would be just:

brew install postgresql

as written here

Answered By: ElmoVanKielmo

If you have anaconda, you can sidestep some of these headaches.

You said you have the Anaconda distribution of python and a quick look at the included packages shows that psycopg2 is already there (although not in the installer). You can simply:

source activate python2
conda install psycopg2

This allows the conda installer to manage all of the binary dependencies. Also makes it easier to upgrade.

If that does not work or there are reasons for not liking that package (version issues?) then that is a different question.

Answered By: Phil Cooper

env is the virtual environment created on Anaconda
once env is activated, type this in the terminal :

conda install -n env [package]

eg.

$ conda install -n env psycopg2

After this type python to enter the Anaconda shell, and then type:

import psycopg2

If it shows no error, it has successfully been installed.

Answered By: storm11

I downloaded anaconda and looks like i have access to pip3
pip3 install psycopg2

Answered By: Monica Puerto

As OP is using Anaconda, if one wants to install psycopg2, open CMD.exe Prompt for the environment that one will be working on and run

conda install -c anaconda psycopg2

# or

conda install psycopg2

(Source)

If one wants to install psycopg2-binary, then use

conda install -c conda-forge psycopg2-binary

# or

conda install psycopg2-binary

(Source)


Notes:

  • If the above didn’t work, consider using pip as follows:

    • pip install psycopg2 (Source)

    • pip install psycopg2-binary (Source)

    Note, however, that pip doesn’t manage dependencies the same way conda does and can, potentially, damage one’s installation.

Answered By: Gonçalo Peres
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.