我在 Mac 上安装了 Postgres,然后安装了 psycopg2,这样我就可以从 Python 访问 Postgres。导入语句不起作用,因为 psycopg2 无法找到某些库的正确版本(我相信)。libpq.dylib 就是这种情况,但首先将 DYLD_LIBRARY_PATH 设置为在 Postgres 目录中搜索解决了该问题:
(base) Jeffs-MacBook-Pro-2:~ jeffsidell$ echo $DYLD_LIBRARY_PATH
/Library/PostgreSQL/11/lib/:/usr/local/lib:/usr/lib
(base) Jeffs-MacBook-Pro-2:~ jeffsidell$ echo $LD_LIBRARY_PATH
/Library/PostgreSQL/11/lib/:/usr/local/lib:/usr/lib
>>> import psycopg2
ImportError: dlopen(/Users/jeffsidell/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libJPEG.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
我在我的 Postgres 安装中查找了 libJPEG.dylib 的其他(较新)版本,但它似乎不包含在 Postgres 中:
(base) Jeffs-MacBook-Pro-2:~ jeffsidell$ cd /Library/PostgreSQL/11/lib
(base) Jeffs-MacBook-Pro-2:lib jeffsidell$ ls *JPEG*
ls: *JPEG*: No such file or directory
检查 /usr/local/lib,发现 libjpeg.dylib 确实存在,因此很可能是版本问题。(对于那些像我一样想知道为什么 libjpeg.dylib == libJPEG.dylib 的人,请参阅这里。
我尝试重新安装 psycopg2,但问题并未得到解决。
(更新):我在 Postgres 子目录中找到了 libjpeg.dylib 的一个版本,因此将其添加到 DYLD_LIBRARY_PATH:
echo $DYLD_LIBRARY_PATH
/Library/PostgreSQL/11/lib/:/Library/PostgreSQL/11/stackbuilder.app/Contents/Frameworks/:/usr/local/lib:/usr/lib/
现在,Postgres 版本的 libjpeg.dylib 似乎与另一个库 libtiff.dylib 存在不兼容,后者除了 /usr/local/lib 之外没有出现在任何地方:
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jeffsidell/anaconda3/lib/python3.7/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import ( # noqa
ImportError: dlopen(/Users/jeffsidell/anaconda3/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.9.dylib
Referenced from: /usr/local/lib/libTIFF.dylib
Reason: Incompatible library version: libTIFF.dylib requires version 13.0.0 or later, but libJPEG.dylib provides version 11.0.0
谢谢你的帮助。