Error importing psycopg2 on M1 mac: incompatible architecture

Viewed 581

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?

3 Answers

For Mac M1 users;

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

This will solve the problem.

Thanks

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

These commands resolved the error:

enter image description here

Related