Problems with Psycopg2 on MAC

Asked

Viewed 156 times

2

I’m getting beat up with Python + Postgre and Psycopg2.

I installed Python 3.4 on my MAC, then Postgre and then followed the following steps:

export PATH=/Library/PostgreSQL/9.4/bin:$PATH
pip install psycopg2

He installed Psycopg 2, but when I give an import I get the following error:

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/leandro/PycharmProjects/Teste/teste.py
Traceback (most recent call last):
  File "/Users/leandro/PycharmProjects/Teste/teste.py", line 1, in <module>
    import psycopg2
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/__init__.py", line 50, in <module>
    from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.1.0.0.dylib
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psycopg2/_psycopg.so
  Reason: image not found

Process finished with exit code 1

On the web I found references to do the following:

sudo mv /usr/lib/libpq.5.dylib /usr/lib/libpq.5.dylib.old 
sudo ln -s /Applications/Postgres.app/Contents/Versions/9.4/lib/libpq.5.dylib /usr/lib

or

sudo ln -s /Library/PostgreSQL/9.4/lib/libpq.5.dylib /usr/lib

But it didn’t solve ... Some hint?

  • My tip would be to install all this in an isolated and not direct environment on Mac OS, look for virtualenv.

  • I also recommend using virtualenv, create a venv with python3 and try running Pip with active venv, most likely solve your problem.

  • In virtualenv he complains that he does not find pg_config although he has already set the path in path.

1 answer

0

I don’t work with Psycopg2 then my answer is more generic, which can help with this kind of problem.

How are you adding something to PATH before installing, it is possible that the same modification is necessary permanently, or at least always before running some program that will use that component.

A simple alternative is to create or modify your file .bashrc (in /Users/leandro/. bashrc) adding that same line:

export PATH=/Library/PostgreSQL/9.4/bin:$PATH

Open a new Terminal tab or window.

If you have replaced your Mac’s default Python with 3.4, I would suggest running the pip with sudo:

sudo pip install psycopg2

If you, on the other hand, are using virtualenv, remember to activate it before installing:

cd ~/PycharmProjects/Teste
source nome-do-env/bin/activate
pip install psycopg2

I imagine now you can run your program in that same Terminal:

sudo python ~/PycharmProjects/Teste/teste.py

Another option. How you seem to be utilizing Pycharm, take a look at the execution settings: menu Run > Edit configurations.... Find the configuration you are using to run your program, edit the field Environment variables, adding a new value: Name = PATH, Value = /Library/PostgreSQL/9.4/bin:$PATH. So, whenever Pycharm runs your program, the value of PATH will be the same modified value you used to install Psycopg2.

  • Thanks! These were the steps I followed, I did the installation in a virtualenv and outside. I tested on two different machines and the problem with OSX seems to be the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.