when I run Django project or any code related to Postgres :
Referenced from: '/Users/mahmoudnasser/.local/share/virtualenvs/wyspp_backend-PwdII1PB/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so'
Reason: tried: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file)
I tried many solutions online but none of them worked.
Note: I use MacOS
CodePudding user response:
To solve this problem just run the following command:
sudo mkdir -p /usr/local/lib && sudo ln -s /opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /usr/local/lib/libpq.5.dylib
CodePudding user response:
Something similar happened to me following a brew
PostgreSQL upgrade. The solution to my problem was to delete my virtual environment, in my case .venv
, and rerun:
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements.txt
After that, I was able to start my application with no problem.
I think the issue was the result of an outdated dependency graph. Re-installing the dependencies with pip found the new version of Postgres and linked the libpq.5.dylib
correctly.
Note, I was using the following psycopg2
dependency:
psycopg2-binary==2.9.3
For what it's worth, I am also on MacOS Monterey
and this just happened in a second codebase on the same machine.
The exact error was:
ImportError: dlopen(/Users/username/dev/src/project/.venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so, 0x0002): Library not loaded: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib'
Referenced from: '/Users/username/dev/src/project/.venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so'
Reason: tried: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file), '/opt/homebrew/Cellar/postgresql@14/14.5_4/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file)