44

I try to install psycopg2 in a virtualenv using pip, the compilation looks ok, it says "Successfully installed psycopg2", but when I try to import it in python interpreter (in virtualenv), it indicates error:

  File "<stdin>", line 1, in <module>
  File "/Users/me/sites/env/trackmap/lib/python2.7/site-packages/psycopg2/__init__.py", line 67, in <module>
    from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Users/me/sites/env/trackmap/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.dylib
  Referenced from: /Users/me/sites/env/trackmap/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Reason: Incompatible library version: _psycopg.so requires version 1.0.0 or later, but libssl.0.9.8.dylib provides version 0.9.8

The thing is I install it successfully using pip in my other virtual env before, like, several weeks ago, and get it working with the postgresql on my mac. I am wondering if this is a problem of compiler? I saw some warnings like shortens from 64-bit to 32-bit during the installation of psycopg2. My compiler is i686-apple-darwin11-llvm-gcc-4.2, default one on mac os x lion.

I see several posts related to psycopg2 install but most of them are solved by installing in a virtual env. So...could anyone give me a suggestion? Thank you! Really appreciated.

p.s. If you need the compilation log of installing psycopg2 please let me know, I didn't paste it here because it is too long.

Yulong
  • 1,529
  • 1
  • 17
  • 26

10 Answers10

102

Does the error say libssl.dylib version is too old?

On my mac, the error is that libssl.dylib and libcrypto.dylib is too old for pyscopg to use. The openssl package used by mac is 0.98, while pyscopg needs 1.0.0 or later version.

My solution is this:

  1. install openssl from brew

    $ brew install openssl
    
  2. copy libssl.1.0.0.dylib and libcrypto.1.0.0.dylib from /usr/local/Cellar/openssl/1.0.1c to /usr/lib/

    $ cd /usr/local/Cellar/openssl/1.0.1c/lib
    $ sudo cp libssl.1.0.0.dylib libcrypto.1.0.0.dylib /usr/lib/
    
  3. in /usr/lib directory, make a softlink libssl.dylib and libcrypto.dylib. You may have to remove the existing links.

    $ sudo rm libssl.dylib libcrypto.dylib
    $ sudo ln -s libssl.1.0.0.dylib libssl.dylib
    $ sudo ln -s libcrypto.1.0.0.dylib libcrypto.dylib
    
azz
  • 5,852
  • 3
  • 30
  • 58
ming.kernel
  • 3,365
  • 3
  • 21
  • 32
  • 3
    FYI from http://stackoverflow.com/questions/11538249/python-pip-install-psycopg2-install-error - "Per Purrell's answer, make sure you include the -fs flags when you use the ln command, which helps ensure that the libraries don't go missing for a short period of time." – Matthew Cornell Dec 10 '12 at 20:15
  • 1
    Thank you for this! I had the linking error but totally unrelated to postgresql, took me forever to find an easy solution. – smessing Apr 08 '13 at 04:26
  • Steps explained pretty nicely at this link http://mithun.co/hacks/library-not-loaded-libcrypto-1-0-0-dylib-issue-in-mac/ – Samar Panda Sep 13 '14 at 15:34
  • Just a note: I had `openssl/1.0.2h_1/`, but the files I needed to move were still `libssl.1.0.0.dylib` and `libcrypto.1.0.0.dylib`. Everything worked great, though I did need [help with sudo permissions](http://stackoverflow.com/questions/32659348/operation-not-permitted-when-on-root-el-capitan-rootless-disabled) as well. – dwanderson Aug 12 '16 at 15:05
  • How about this? http://initd.org/psycopg/docs/install.html#binary-install-from-pypi – Ajay Rawat May 05 '19 at 06:44
  • 4
    In recent versions of MacOS, `/usr/lib` is protected by SIP. Following these instructions with `/usr/local/lib` worked for me. – Aneel May 20 '19 at 00:15
56

I think in Mac we need:

export LDFLAGS="-L/usr/local/opt/openssl/lib"

on m1:

export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
kev
  • 8,928
  • 14
  • 61
  • 103
jiamo
  • 1,406
  • 1
  • 17
  • 29
25

I had a similar issue. I had used Anaconda to install python and a number of packages and then later used pip to install psycopg2. I was able to fix the error by uninstalling psycopg2 and reinstalled it with the conda package manager instead of pip.

pip uninstall psycopg2
conda install psycopg2
ansonw
  • 1,559
  • 1
  • 16
  • 22
24

On OSX 10.11+ (El Capitan or later) solution with replacing symlinks (see above - https://stackoverflow.com/a/11911870/565525) sudo rm libssl.dylib libcrypto.dylib reports Operation not permitted. Solution that worked for me was:

brew install openssl

Find where openssl brew libs are located, start searching from directory /usr/local/Cellar/openssl. In my case it is in /usr/local/Cellar/openssl/1.0.2d_1/lib

Finally set up DYLD_LIBRARY_PATH, i.e. add a line like this into .bash_profile :

# replace location of lib files with folder name you found in previous step
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/Cellar/openssl/1.0.2d_1/lib

restart shell, or just source ~/.bash_profile and test if it works:

$ python -c"import psycopg2  ;   print('psycopg2 is now ok')"
Community
  • 1
  • 1
Robert Lujo
  • 15,383
  • 5
  • 56
  • 73
13

Thanks @khrf for your answer.

If you are using the postgres system installer (instead of Postgres.app), use:

export DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.3/lib

which can be placed in your ~/.bash_profile if you want to avoid typing it every time.

gozzilli
  • 8,089
  • 11
  • 56
  • 87
  • This solution didn't work for me. Instead I try to place to `~/.bash_profile` : `export DYLD_FALLBACK_LIBRARY_PATH=/Library/PostgreSQL/9.4/lib:$DYLD_FALLBACK_LIBRARY_PATH` , which it worked. – Marcellinov Jul 25 '15 at 16:00
1

If you are using PostgresApp, set DYLD_LIBRARY_PATH environment variable accrordingly, e.g.:

DYLD_LIBRARY_PATH=/Applications/Postgres.app/Contents/MacOS/lib/ ../venv/bin/python manage.py syncdb
wobmene
  • 1,108
  • 9
  • 14
1

Add below line in ~/.bash_profile

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

Then

source ~/.bash_profile
pyAddict
  • 1,576
  • 13
  • 15
1

The below method works for me.

brew install zlib
brew install bzip2
brew install openssl
export LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/bzip2/lib -L/usr/local/opt/openssl@3/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/openssl@3/include"
0

After a few hours troubleshooting, the following line added to my .zshrc file helped me out

export DYLD_LIBRARY_PATH=/Library/PostgreSQL/11/lib
Zeca
  • 71
  • 1
  • 1
  • 12
0

Find where your libssl and libcrypto files are and set the LIBRARY_PATH in your zshrc file would work.

References: Add library search path to clang https://mithun.co/hacks/library-not-loaded-libcrypto-1-0-0-dylib-issue-in-mac/

xyzgentoo
  • 1
  • 1