Adding PostgreSQL support to Enthought's Canopy on OS X

How to install psycopg2 into Canopy's virtualenv using brew

Although Enthought’s Canopy Python distribution for OS X is fairly complete, it doesn’t have the psycopg2 module and it is not available on the community repository.

Before you start

Some readers pointed out that PostgreSQL might already be installed, so before you do anything, check whether ‘’pg_config’’ is present in your system. If it is, you can just skip the next section, as you only need to install ‘’psycopg2’’ using pip. (Thanks RobinD63!)

Installing dependencies

Given that Canopy is a full fledged Python environment, we could try to install the module using pip, but before we can do that we need to make sure that we have Xcode’s command line utilities installed. The easiest way to do this, is to install Homebrew. Use this command, and follow the instructions on the screen:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

If try to install psycopg2 with pip now, it’ll fail with the following message:

Error: pg_config executable not found

This is because psycopg2 depends on PostgreSQL client tools. Sadly, these aren’t available as a separate package, so we need to install a PostgreSQL server just to have access to the pg_config executable. We cannot use Homebrew to install PostgreSQL because it is not compatible with Canopy’s Python environment, and Python is required to build PostgreSQL.

In order to install a full PostgreSQL distribution you can use EnterpriseDB’s PostgreSQL graphical installer. All you need to do is follow the instructions on screen, unless you’re planning on actually using the server and not just the client libraries, in which case, pay attention to the configuration dialogs and remember the passwords you set for the ‘’postgres’’ user.

If you really don’t need a PostgreSQL server running, you can just install Postgres.app.

Installing psycopg2

Once PostgreSQL is installed, we can build the psycopg2 module. If you chose the EntrerpiseDB installer use:

$ PATH=$PATH:/Library/PostgreSQL/9.3/bin/ pip install psycopg2

or:

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

if you used Postgres.app. In either case, if all goes well you’ll see the following message:

Successfully installed psycopg2
Cleaning up...

Let’s see if the module works properly. Open a python (or an ipython) shell, and import the psycopg2 module:

In [1]: import psycopg2
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-bd284aa2cf56> in <module>()
----> 1 import psycopg2

/Users/guest/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/psycopg2/__init__.py in <module>()
     48 # Import the DBAPI-2.0 stuff into top-level module.
     49
---> 50 from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
     51
     52 from psycopg2._psycopg import Binary, Date, Time, Timestamp

ImportError: dlopen(/Users/guest/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.1.0.0.dylib
  Referenced from: /Users/guest/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Reason: image not found

The module is failing to import because it depends on OpenSSL. In order to install it, we’re going to use brew:

$ brew install openssl
$ brew link -f openssl

You can safely ignore any warning messages brew gives you. Let’s try to import psycopg2 once again:

In [1]: import psycopg2

In [2]:

And with that, we added support for PostgreSQL to Enthought’s Canopy Python distribution on OS X.


Previous / Next posts


Comments