Saki's 研究记录

(MAC Big Sur 12.1) ImportError: dlopen libpq

字数统计: 203阅读时长: 1 min
2022/01/26

问题

python脚本中连接postgres时遇到以下问题:

1
2
3
4
5
6
7
8
Traceback (most recent call last):
File "/Users/shenshijie/Documents/app/postgres/connect.py", line 2, in <module>
import psycopg2
File "/opt/homebrew/lib/python3.9/site-packages/psycopg2/__init__.py", line 51, in <module>
from psycopg2._psycopg import ( # noqa
ImportError: dlopen(/opt/homebrew/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 0x0002): Library not loaded: /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
Referenced from: /opt/homebrew/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so
Reason: tried: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file)

解决

在我的Mac BigSur 12.1 M1遇到以上问题,按顺序执行以下命令顺利解决:

1
2
3
4
$ pip3 uninstall psycopg2
$ brew install libpq --build-from-source
$ echo 'export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"' >> ~/.zshrc
$ pip3 install psycopg2 --no-cache-dir

以上。

CATALOG
  1. 1. 问题
  2. 2. 解决