Psycopg2 image not found

Trying to setup postgres with the postgres mac app and hit this error, which I haven't been able to solve. Any thoughts?

    ImportError: dlopen(/Users/Craig/pyenv/mysite/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: @executable_path/../lib/libssl.1.0.0.dylib
Referenced from: /Applications/Postgres.app/Contents/MacOS/lib/libpq.dylib
Reason: image not found
40884 次浏览

Did you install psycopg2? You need it in order to integrate python with postgresql.

In your bash environment before you load it, try this:

export DYLD_LIBRARY_PATH=/Library/PostgreSQL/x.y/lib

..replacing the 'x.y' with the version on your system.

..be aware that setting this in your bash profile can interfere with other programs, as KindOfGuy noted.

..of course, if you're not running it from a bash prompt, you'll have to set up your environment in whatever way pyenv lets you. ..you could even edit pyenv itself and place that at the top.

Another alternative is to put this in a python script which runs before you attempt to import psycopg2:

import os
os.environ['DYLD_LIBRARY_PATH'] = '/Library/PostgreSQL/x.y/lib'

..again, replacing 'x.y' with the version on your system in /Library/PostgreSQL.

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

I encountered this error while working on Django. I have it working on virtualenv with Django==1.3 but not on Django==1.5 where I have to issue the commands above.

In OS X El Capitan you can't do these links without disabling system protection but it works well if you link to /usr/local/lib

I found a solution that worked for me when dealing with a similar issue on rails. Add the following to your .bash_profile, .bash_rc, or equivalent:

export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Postgres.app/Contents/MacOS/lib:$DYLD_LIBRARY_PATH

(Assuming you installed Postgres.app in the default location). Then restart your terminal session and try again.

Exporting to DYLD_LIBRARY_PATH directly can cause serious problems with other apps that depend on it, but using the fallback path avoids these problems.

See also: https://github.com/PostgresApp/PostgresApp/issues/109#issuecomment-18387546

EDIT: It seems that setting DYLD_FALLBACK_LIBRARY_PATH causes an error when you try to run psql. To fix this, you can add the following two lines to your .bash_profile:

alias psql="(. ~/.bash_profile; unset DYLD_FALLBACK_LIBRARY_PATH; psql)";

This is assuming that you're using bash and that your .bash_profile is located in your home directory. If that's not the case (or if you're using a .bashrc or other environment setup instead of .bash_profile) change the ~/.bash_profile part of the command to the path to your environment setup script.

The aliased command basically starts a subshell which does not effect your current bash environment. So when it unsets the DYLD_FALLBACK_LIBRARY_PATH variable, it's only temporary. After you exit psql the environment variable will be set again.

This problem cost me the whole morning to solve. I found the discussion on http://initd.org/psycopg/articles/2010/11/11/links-about-building-psycopg-mac-os-x/ really helpful. Thanks to Jurie's answer, the solution to my problem (in Mac) is as below:

  1. Install openssl 1.0.0 using brew:

     brew install openssl
    
  2. using the following command:

    export DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/**1.0.1x**/lib
    

    replace 1.0.1x part with your current version. For me it is 1.0.1h.

Hope this helps!

EDIT: After one day, I found that the second command has to be entered every time when needing to connect to database, so not a permanent solution to this problem.

I found this solution that worked for me

sudo cp /Applications/Postgres.app/Contents/Versions/9.3/lib/libssl.1.0.0.dylib /usr/lib
sudo ln -fs /usr/lib/libssl.1.0.0.dylib /usr/lib/libssl.dylib


sudo cp /Applications/Postgres.app/Contents/Versions/9.3/lib/libcrypto.1.0.0.dylib /usr/lib
sudo ln -fs /usr/lib/libcrypto.1.0.0.dylib /usr/lib/libcrypto.dylib

Replace this part '/Applications/Postgres.app/Contents/Versions/9.3/' depending upon the location where psql is installed on your machine. Command to find where psql is installed :which psql

UPDATE FROM COMMENTS: On OSX 10.11 (El Capitan), you can no longer copy files to /usr/lib. Use /usr/local/lib

I was missing the postgresql client package, so I installed them with brew and that fixed this issue for me.

brew update
brew doctor
brew install postgresql

Big picture, the problem is that a required library couldn’t be found. You can alter where psycopg2 looks for libssl using Apple’s open source compiler tools, otool and install_name_tool. These ship with OS X, and manual pages are available with man <command>.

Change into the psycopg2 module directory mentioned in the error message. Once there:

$ otool -L _psycopg.so
...
@executable_path/../lib/libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
...

This lists the libraries _psycopg2.so will look for. You can change where it’ll look with install_name_tool:

$ install_name_tool -change @executable_path/../lib/libssl.1.0.0.dylib /usr/local/opt/openssl/lib/libssl.1.0.0.dylib _psycopg.so

You’ll need to adjust for where you have libssl.1.0.0.dylib, of course. The example I gave is the default Homebrew path, but you might have it from Anaconda and/or the PostgreSQL app bundle. (brew install openssl if you don’t have it yet.) You’ll also likely need to repeat for libcrypto.

Executing this change may fail depending on how _psycopg2.so was built. If that happens, you could probably build the module yourself with custom library paths, but I won’t get into that.

This approach has the advantage of being narrower, and thus less risky, than the approach (given in other answers here) of linking libssl 1.0.0 into dyld’s search paths (either through ln -s or setting a DYLD_* environment variable). (See warnings against these approaches in a pair of discussions and some code. Learn more about dyld through man dyld.) It has the disadvantage of needing to be repeated for each copy of psycopg2. Choose your own adventure.

Disclaimer: Most of the content in this answer is from knowledge I cobbled together in one day. I am no expert.

I had the same error because of upgrading Postgres.app (from 9.3 to 9.4).

The solution was to delete the pip cache wheels because they were pointing to Postgres.app version 9.3.

I've found the according psycopg wheel cache files this way

grep -r psycopg ~/.pip/cache

and deleted the directory I've found with the last command.

This happened to me after upgrading Postgresql, and after installing psycopg2 in my virtualenv. Reinstalling (re-building) worked for me.

pip uninstall psycopg2
pip install psycopg2

I encountered this issue when I upgraded from postgres.app 9.4 to 9.5 on el capitan.

The other solutions will not work (easily) in el capitan because of the system lock on certain directories, meaning the symbolic link solution will be less accessible/ideal for most.

This leaves the fallback variable. The current answer points to the wrong directory. I am guessing the dylibs directory has changed since 2013.

So, here is the latest fallback directory that works for me:

export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Postgres.app/Contents/Versions/latest/lib:$DYLD_LIBRARY_PATH

Under Mac OS X 10.11 (El Capitan), /usr/lib is read only for root user. You'll get a ln: /usr/lib/libssl.1.0.0.dylib: Operation not permitted error You need to use /usr/local/lib instead of /usr.

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

I'm experiencing a similar issue with mysql. The library requested is is failing to load /usr/local/mysql/lib/libmysqlclient_r.16.dylib

However, in this directory, there is a later version of the library: /usr/local/mysql/lib/libmysqlclient.20.dylib

I am working in a virtualenvwrapper and have attempted to reinstall all of the previously install dependencies to no avail. Any assistance would be greatly appreciated.

Just wanted to share what worked for me. I was using Anaconda with jupyter. The following did work:

 DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/1.0.2k/lib  jupyter notebook

But then caused later issues importing another library (plotly). After playing around with pip, I realised I should probably be using conda instead and the following seemed to fix everything.

conda install psycopg2

The solution that worked for me was to install psycopg2 with the "no-binary" option set, which tells psycopg2 to use the system libssl.

By default psycopg2 uses its own version of libssl and it seemed to keep a dependency on an old version of the library which no longer existed after my upgrade.

See the psycopg2 install docs for more detail about the no-binary option.

pip uninstall psycopg2
pip install --no-binary :all: psycopg2

Try updating psycopg2 using pip install psycopg2 --upgrade to the latest version, then try again.

try finding the file or files libssl.1.0.0.dylib and copy them to your '/usr/local/lib/' for me the origin was, '/Library/PostgreSQL/11/lib/' ,(change the version to yours) alternatively you may copy all to '/usr/local/lib/' go to the origin and type: sudo cp * /usr/local/lib/ to copy all files. I found the solution and changed it to solve mine https://oscarvalles.wordpress.com/2017/03/24/dlopenlibrary-not-loaded-libssl-dylib-fail-to-import-psycopg2-in-virtualenv/

pip install psycopg2-binary

works like a charm!

Discussion on source+wheel distribution (pyscopg2) vs a separate binary distribution (psycopg2-binary): https://www.postgresql.org/message-id/CA%2Bmi_8bd6kJHLTGkuyHSnqcgDrJ1uHgQWvXCKQFD3tPQBUa2Bw%40mail.gmail.com

Explanation on the decision to release psycopg2-binary: http://initd.org/psycopg/articles/2018/02/08/psycopg-274-released/

However, there are reports of psycopg2-binary not satisfying psycopg2 dependencies. More discussion here: https://github.com/psycopg/psycopg2/issues/674

Use psycopg2-binary instead

pip install psycopg2-binary

In case if you are working on python/django with virtual environment

Worked for me for the following Version

  • MacOS Catalina 10.5.2
  • python 3.7.3
  • Django 3.0.2
  • psycopg2==2.8.4
  • virtualenv 16.6.0
  • pip 19.3.1
  • postgres (PostgreSQL) 11.2

3 Steps Solutions

  • Delete Existing Virtual Environment
  • Create New Virtual Environemnt again
  • Install Dependencies again

I was encountering similar problem while running my Django server with postgreSQL as DB. I fixed it by relying on following steps, I am making use of conda to take care of virtual enviroments.

Create a new virtual enviroment I named it newenv

1 - conda create --name newenv

2 - conda activate newenv

3 - pip install -r requirements.txt

4 - python manage.py runserver

Afterwards It worked like a charm :).

I encountered a similar problem I am using macOS executing pip install psycopg2-binary did wonder for me :)