19

I've got a python django dev setup on my mac and have just upgraded to El Capitan.

I've got psycopg2 installed in a virtualenv but when I run my server I get the following error -

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/aidan/Environments/supernova/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.1.0.0.dylib
  Referenced from: /Users/aidan/Environments/supernova/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Reason: image not found

I've tried reinstalling my virtualenv

pip install -f requirements.txt

And I've tried upgrading psycopg2

pip uninstall psycopg2
pip install psycopg2

But I'm still getting the same error.

I've also tried adding symlinks to /usr/lib but El Capitan's new rootless thing doesn't allow it -

$ sudo ln -s /Library/PostgreSQL/9.2/lib/libssl.1.0.0.dylib
 /usr/lib
ln: /usr/lib/libssl.1.0.0.dylib: Operation not permitted

So I tried /usr/local to no avail.

The system version of openssl seems to be 1.0.2 -

$ openssl version
OpenSSL 1.0.2d 9 Jul 2015

How do I fix this?

Community
  • 1
  • 1
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
  • I had this same frustrating issue yesterday. Did some of this same troubleshooting, no avail. I restarted my comp this morning and... like magic, psycopg2 is working again. Probably doesn't help one bit, but I thought I would at least say something. – Darec Oct 07 '15 at 13:43
  • sorry I didn't get, what happened when you tried linking it into `/usr/local/lib` ? – haynar Oct 07 '15 at 16:19

6 Answers6

23

I tried the following:

I have brew installed on my machine. Running $ brew doctor gave me a suggestion to do the following:

$ sudo chown -R $(whoami):admin /usr/local

Once this was done, I re-installed psycopg2 and performed the following:

$ sudo ln -s /Library/PostgreSQL/9.3/lib/libssl.1.0.0.dylib /usr/local/lib/
$ sudo ln -s /Library/PostgreSQL/9.3/lib/libcrypto.1.0.0.dylib /usr/local/lib/

Please note the version of your PostgreSQL and the path /usr/local/lib.

Doing this got me back to a working setup again.

P.S.: The brew suggested step might not be relevant here but I put this up because you were seeing permission issues. You could also disable rootless mode.

Community
  • 1
  • 1
Nandeep Mali
  • 4,456
  • 1
  • 25
  • 34
18

The reason is indeed psycopg2 using relative paths to a few PostgreSQL libraries. To fix it I used

sudo install_name_tool -change libpq.5.dylib /Library/PostgreSQL/9.4/lib/libpq.5.dylib /Library/Python/2.7/site-packages/psycopg2/_psycopg.so
sudo install_name_tool -change libcrypto.1.0.0.dylib /Library/PostgreSQL/9.4/lib/libcrypto.1.0.0.dylib /Library/Python/2.7/site-packages/psycopg2/_psycopg.so
sudo install_name_tool -change libssl.1.0.0.dylib /Library/PostgreSQL/9.4/lib/libssl.1.0.0.dylib /Library/Python/2.7/site-packages/psycopg2/_psycopg.so

to convert the relative paths to absolute ones. Of course you'd need to do this every time you reinstall psycopg2.

ArtHarg
  • 212
  • 1
  • 8
  • 1
    this worked great for me and also agrees with the reason in the error message (unsafe use of rpath) – neal Oct 31 '15 at 19:17
  • 1
    Filed as a bug to see if it can be fixed upstream: https://github.com/psycopg/psycopg2/issues/385 – Chris Withers Dec 23 '15 at 18:49
  • This worked for me as well, after reinstalling packages, wiping out the conda env and recreating, and messing with `DYLD_LIBRARY_PATH` and `DYLD_FALLBACK_LIBRARY_PATH`, but only this worked (confusingly enough, I was able to use the library paths in `/usr/local/lib`). I'm still not really sure why this is necessary, and seemingly just in El Cap? – Joshua Gross Nov 28 '16 at 07:17
  • El Cap introduces SIP and SIP is very strict about relative paths leading to libraries in paths outside SIP's control. This is so you are not inadvertently made to use a compromised library. The change to absolute paths tells SIP "It's okay, I know what I am doing. I trust these specific libraries." – ArtHarg Feb 09 '17 at 08:54
  • 1
    From the issue filed by @ChrisWithers and his own solution in the end, this works, `pip install -i https://testpypi.python.org/pypi psycopg2==2.7b1`. So I guess we have to wait for version 2.7 to come out? Currently it's at `2.6.2` – RAbraham Feb 13 '17 at 20:50
7

Do you use anaconda? If so a very simple solution that worked for me was to simply install using conda.

Specifically, enter the following in at the command line:

conda install psycopg2

MV22
  • 191
  • 2
  • 7
  • Please do not post [duplicate answers](//meta.stackexchange.com/a/211726/206345). Instead, consider other actions that could help future users find the answer they need, as described in the linked post. – Mogsdad Apr 19 '16 at 00:50
  • 1
    This is the result of redundant questions. While coming up with a million different solutions to the same problem would be incredibly academic, it is not practical. The real problem here is that identical question threads should be consolidated into a single post. – MV22 Apr 19 '16 at 02:33
  • That is indeed one of the options. Users can [flag duplicate posts](http://stackoverflow.com/help/privileges/flag-posts), or [vote to close them as duplicates](http://stackoverflow.com/help/privileges/close-questions). Alternatively, you could leave a comment to point to another answer. – Mogsdad Apr 19 '16 at 03:03
1

Based on @ArtHarg solution, i used this with python 3.4, PostgreSQL 9.5 and OS X el capitan:

sudo install_name_tool -change libpq.5.dylib /Library/PostgreSQL/9.5/lib/libpq.5.dylib /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so
sudo install_name_tool -change libcrypto.1.0.0.dylib /Library/PostgreSQL/9.5/lib/libcrypto.1.0.0.dylib /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so
sudo install_name_tool -change libssl.1.0.0.dylib /Library/PostgreSQL/9.5/lib/libssl.1.0.0.dylib /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so
Mohsenasm
  • 2,916
  • 1
  • 18
  • 22
0

This is a pain for me too, because i used to set

export DYLD_FALLBACK_LIBRARY_PATH=/Library/Postgresql/9.5/lib

in my .bash_profile, and everything worked OK. But now that environment variable is one of the ones that can't be passed into new shells. It means I can import psycopg2 fine in python run from my login shell, but my tests scripts die.

I now set $SIP_WORKAROUND_LIBRARY_PATH to the same value, which OSX will leave alone, and then I have to do:

export DYLD_FALLBACK_LIBRARY_PATH=$SIP_WORKAROUND_LIBRARY_PATH

in my test scripts, to sneak the value through. It's far from ideal, but I hope it helps someone.

scav
  • 1,095
  • 1
  • 9
  • 17
0

I'm running El Capitan 10.11.5 and python 3.5.1 and have hit the same error with trying to import psycopg2, either in a simple console or within PyCharm. I managed to fix this by getting to root:

sudo su -

then symbolically linking two libraries:

ln -s /Library/PostgreSQL/9.5/lib/libssl.1.0.0.dylib /usr/local/lib
ln -s /Library/PostgreSQL/9.5/lib/libcrypto.1.0.0.dylib /usr/local/lib

I then experienced a connection error with a message that ended:

psycopg2.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

My PostgreSQL install was listening on a socket in /tmp so another symlink was needed to allow connection:

ln -s /tmp/.s.PGSQL.5432 /var/pgsql_socket/