Error importing psycopg2 on M1 mac: incompatible architecture
Asked Answered
D

4

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?

Detergency answered 6/9, 2022 at 17:30 Comment(0)
E
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
Etesian answered 13/9, 2022 at 9:3 Comment(2)
both Utku's and cgl's solutions worked for me. I tried both, this one is more to my liking.Shelton
Since my package was psycopg2, pip install psycopg2 --force-reinstall --no-cache-dir This fixed the problem.Freddyfredek
C
6

For Mac M1 users;

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

This will solve the problem.

Thanks

Curcuma answered 6/9, 2022 at 20:1 Comment(0)
M
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

Monocotyledon answered 26/9, 2022 at 11:42 Comment(0)
H
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
Herbivorous answered 11/8, 2023 at 10:38 Comment(2)
Avoid code only asnwer and provide an explanation.Buffington
I had to do it the other way around, but this works.Warring

© 2022 - 2024 — McMap. All rights reserved.