在 M1 mac 上导入 psycopg2 时出错:架构不兼容

Han*_*nna 8 python macos psycopg2 apple-m1

当我尝试导入 psycopg2 时,收到以下错误消息:

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)
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个错误?

cgl*_*cgl 34

我在升级 Postgres 时遇到了同样的问题。重新安装psycopg2-binary解决了这个问题,因为这对我来说是一个缓存问题。

pip install psycopg2-binary --force-reinstall --no-cache-dir
Run Code Online (Sandbox Code Playgroud)

  • Utku 和 cgl 的解决方案都对我有用。两种我都试过了,这个更合我的口味。 (2认同)

Utk*_*Can 7

对于 Mac M1 用户;

pip install -i https://test.pypi.org/simple/ psycopg2-binary==2.9.3
Run Code Online (Sandbox Code Playgroud)

这将解决问题。

谢谢