在 macOS 上安装 psycopg2 失败

6

我已经通过brew在macOS上安装了PostgreSQL

brew install postgresql

尝试了以下命令,但都失败了:
  • pipenv install psycopg2
  • pip install psycopg2
错误信息:
...
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/adapter_qstring.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_qstring.o
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/microprotocols.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols.o
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/microprotocols_proto.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols_proto.o
  In file included from psycopg/microprotocols_proto.c:27:
  In file included from ./psycopg/psycopg.h:37:
  ./psycopg/config.h:81:13: warning: unused function 'Dprintf' [-Wunused-function]
  static void Dprintf(const char *fmt, ...) {}
              ^
  1 warning generated.
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/typecast.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/typecast.o
  clang -bundle -undefined dynamic_lookup -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/user0/.pyenv/versions/3.8.0/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/user0/.pyenv/versions/3.8.0/lib build/temp.macosx-10.14-x86_64-3.8/psycopg/psycopgmodule.o build/temp.macosx-10.14-x86_64-3.8/psycopg/green.o build/temp.macosx-10.14-x86_64-3.8/psycopg/pqpath.o build/temp.macosx-10.14-x86_64-3.8/psycopg/utils.o build/temp.macosx-10.14-x86_64-3.8/psycopg/bytes_format.o build/temp.macosx-10.14-x86_64-3.8/psycopg/libpq_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/win32_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/solaris_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/connection_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/connection_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/cursor_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/cursor_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/column_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_connection_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_cursor_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_message_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/diagnostics_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/error_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/conninfo_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/lobject_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/lobject_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/notify_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/xid_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_asis.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_binary.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_datetime.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_list.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pboolean.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pdecimal.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pint.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pfloat.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_qstring.o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols.o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols_proto.o build/temp.macosx-10.14-x86_64-3.8/psycopg/typecast.o -L/usr/local/lib -lpq -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.8/psycopg2/_psycopg.cpython-38-darwin.so
  ld: library not found for -lssl
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  error: command 'clang' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for psycopg2

此外,将其设置为Shell,然后重新加载:

export PATH=/usr/local/Cellar/postgresql/12.1/bin:$PATH

也不起作用。


你是否拥有多个Python版本? - RMPR
@RMPR 是的,我已经通过pyenv安装了多个Python版本。但现在正在使用3.8.0。这有什么关系吗? - rawmain
2
这个回答解决了你的问题吗?使用pip安装mysqlclient报错,找不到-lssl库 - phd
https://stackoverflow.com/search?q=%5Bpip%5D+ld%3A+library+not+found+for+%22-lssl%22 - phd
1
这个回答解决了你的问题吗?pg_config可执行文件未找到 - DUDANF
3个回答

14

首先,您需要验证 openssl 是否已安装并正确链接:

brew install openssl
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

现在,如果使用Python 3.8意味着python可执行文件绑定到Python 3.8,您可能想尝试以下解决方法:

  • 解决方案#1:安装时指定解释器
python -m pip --no-cache install psycopg2

为确保您调用的pip版本确实与您的Python版本绑定,请注意检查。

如果仍然不能工作,您可能会遇到此问题,在这种情况下,您可以降级您的Python版本,然后再次尝试上述命令。

  • 解决方法#2:从源代码安装。

首先,您需要从PyPI下载源代码,并运行以下命令:

tar -xf psycopg2-2.8.4.tar.gz && cd psycopg2-2.8.4 # extract the file
python setup.py build
sudo python setup.py install

1
两种方法都出现了错误:ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib' error: command 'clang' failed with exit status 1. - rawmain
你验证了 OpenSSL 的事情吗? - RMPR
如果这个答案对你有帮助,请不要忘记将其标记为有效的。 - RMPR
4
谢谢!经过八个小时的错误提示,你的建议“EXPORT”终于让psycopg2成功安装了。为什么Python的每一个步骤都要如此失败呢?好像你需要掌握某种秘密握手才能让任何东西正常工作。再次感谢你提供的那个“神秘古老知识”,让我成功安装了psycopg2。又浪费了一天在Python配置上。 - frozenjim
出口解决了在macOS Big Sur上的问题。 - shivg
显示剩余2条评论

7

在这种情况下,最好的解决方案是安装二进制软件包。

  pip install psycopg2-binary

在此之后,您仍可以尝试重新安装较旧版本的psycopg2包,如下所示。如果您不一定需要最新版本,则应该可以正常工作。

  pip install psycopg2==2.7.5

它还出现了“错误:命令'clang'以退出状态1失败”的错误。 - rawmain
@rawmain 安装命令行工具可能有助于解决问题,可以这样做:xcode-select --install。看到完整的错误消息会很有帮助。但是这个线程也很相似:https://dev59.com/O-k5XIcBkEYKwwoY7eME - Cami Fandino
请记住 psycopg2-binary 仅推荐在开发过程中使用,不应用于生产环境。 - LondonAppDev

0
  • 尝试降级你的Python3版本
  • 尝试使用pip3

我用这种方法解决了这个问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接