python psycopg2 包在 macbook 上的安装运行问题
在 macbook 上安装 python 的 psycopg2 包后,启动项目会出现类似如下问题:
from psycopg2._psycopg import ( # noqa ImportError: dlopen(/Users/Xproject/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 0x0002): Library not loaded: libpq.5.dylib Referenced from: <BB3D4271-F451-3975-83E8-B106B48E27DA> /Users/Xproject/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so Reason: tried: 'libpq.5.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibpq.5.dylib' (no such file), 'libpq.5.dylib' (no such file), '/Users/Xproject/src/libpq.5.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/Xproject/src/libpq.5.dylib' (no such file), '/Users/Xproject/src/libpq.5.dylib' (no such file)
解决方法如下:
一、在本地安装需要的包 krb5 和 libpq:
brew install --build-from-source krb5 brew install --build-from-source libpq
二、修改应用程序动态库的查找路径:
export PSYCOPG2=/Users/zhenxink/neo/projects/yoda_projects/yoda-api/venv/lib/python3.9/site-packages/psycopg2/ install_name_tool -change libpq.5.dylib /Library/PostgreSQL/12/lib/libpq.5.dylib $PSYCOPG2/_psycopg*.so # /Library/PostgreSQL/12/lib 为 postgresql 在我本地的安装路径 install_name_tool -change libssl.1.1.dylib /Library/PostgreSQL/12/lib/libssl.1.1.dylib $PSYCOPG2/_psycopg*.so install_name_tool -change libcrypto.1.1.dylib /Library/PostgreSQL/12/lib/libcrypto.1.1.dylib $PSYCOPG2/_psycopg*.so
执行以上操作后包含 psycopg2 的项目就能运行起来了。
参考链接:
https://github.com/psycopg/psycopg2/issues/1095
Code your future.