在MacOS上安装Psycopg2失败

时间:2019-12-06 04:37:28

标签: python macos pip psycopg2 pipenv

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

brew install postgresql

尝试了以下命令,所有命令均失败:

  • pipenv安装psycopg2
  • pip安装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

也不起作用。

2 个答案:

答案 0 :(得分:1)

首先,您需要验证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版本绑定即可。

如果仍然无法正常运行,则可能是遇到this问题,在这种情况下,您可以降级Python版本并再次尝试上述命令

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

首先,您需要下载PyPI上托管的source code并运行以下命令:

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 :(得分:1)

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

  pip install psycopg2-binary

之后,您仍然可以尝试像这样重新安装旧版本的psycopg2软件包。如果您不一定需要最新版本,这应该可以工作。

  pip install psycopg2==2.7.5