10

When I try importing psycopg2, I get this error message:

ImportError: dlopen(/opt/homebrew/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: /opt/homebrew/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' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/libpq.5.dylib' (no such file), '/opt/homebrew/Cellar/postgresql@14/14.5_3/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/libpq.5.dylib' (no such file)

Does anyone know how to resolve this error?

4 Answers 4

40

I had the same problem over some Postgres upgrade. Reinstalling psycopg2-binary solved the problem as it was a caching issue for me.

pip install psycopg2-binary --force-reinstall --no-cache-dir
2
  • 2
    both Utku's and cgl's solutions worked for me. I tried both, this one is more to my liking.
    – boar
    Commented Sep 15, 2022 at 14:11
  • 1
    Since my package was psycopg2, pip install psycopg2 --force-reinstall --no-cache-dir This fixed the problem. Commented Oct 27, 2022 at 16:00
6

For Mac M1 users;

pip install -i https://test.pypi.org/simple/ psycopg2-binary==2.9.3

This will solve the problem.

Thanks

4

https://github.com/PostgresApp/PostgresApp/issues/83#issuecomment-1232400894

ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/* /opt/homebrew/opt/postgresql/lib/

worked for me

2

Uninstall the package -x86_64:

arch -x86_64 python3 -pip uninstall psycopg2-binary

Install the package -arm64:

arch -arm64 python3 -m pip install psycopg2-binary
2
  • 3
    Avoid code only asnwer and provide an explanation. Commented Aug 11, 2023 at 13:24
  • I had to do it the other way around, but this works.
    – Fakipo
    Commented Sep 25, 2023 at 1:29

Your Answer

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

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