在 M1 上导入 psycopg2 时出现 ImportError

ImportError when importing psycopg2 on M1

有人在成功安装后导入 psycopg2 时遇到此错误吗?

ImportError: dlopen(/Users/chrishicks/Desktop/test/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 0x0002):
tried: '/Users/chrishicks/Desktop/test/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so'
(mach-o file, but is an incompatible architecture
(have 'x86_64', need 'arm64e')),
'/usr/local/lib/_psycopg.cpython-39-darwin.so' (no such file),
'/usr/lib/_psycopg.cpython-39-darwin.so' (no such file)

我已经尝试安装 psycopg2psycopg2-binary 并且在 运行 iTerm 在 Rosetta 中都尝试过。

使用这一行应该可以解决问题:

pip3.9 install psycopg2-binary --force-reinstall --no-cache-dir