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?

2
  • 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 7, 2015 at 13:43
  • sorry I didn't get, what happened when you tried linking it into /usr/local/lib ?
    – haynar
    Oct 7, 2015 at 16:19

6 Answers 6

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.

4
  • "sudo ls -s" didn't work for me (changed paths accordingly). Sadly, I had to disable the SIP in recovery mode and reboot. Oct 10, 2015 at 20:49
  • Sorry, I didn't get the ls -s bit. You mean ln -s? Oct 11, 2015 at 6:37
  • Yes, sorry! mispelled. Oct 12, 2015 at 21:08
  • 1
    I didn't have to chown /usr/local as it is not protected by the rootless mode Oct 21, 2015 at 8:24
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.

5
  • 1
    this worked great for me and also agrees with the reason in the error message (unsafe use of rpath)
    – neal
    Oct 31, 2015 at 19:17
  • 1
    Filed as a bug to see if it can be fixed upstream: github.com/psycopg/psycopg2/issues/385 Dec 23, 2015 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? Nov 28, 2016 at 7: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 9, 2017 at 8: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, 2017 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

3
  • Please do not post duplicate answers. Instead, consider other actions that could help future users find the answer they need, as described in the linked post.
    – Mogsdad
    Apr 19, 2016 at 0: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, 2016 at 2:33
  • That is indeed one of the options. Users can flag duplicate posts, or vote to close them as duplicates. Alternatively, you could leave a comment to point to another answer.
    – Mogsdad
    Apr 19, 2016 at 3: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
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.

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/

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.