安装psycopg2时出错,找不到lssl的库

2024-05-01 21:15:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我跑

sudo pip install psycopg2

我得到了一组输出,看起来像:

cc -DNDEBUG -g -fwrapv -Os .....
.....
cc -DNDEBUG -g -fwrapv -Os .....
.....

最后它说:

ld: library not found for -lssl

clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: command 'cc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/private/var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip_build_root/psycopg2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip-uE3thn-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /private/var/folders/bz/pvj1g9xj16d10pjjgbrfl3fw0000gn/T/pip_build_root/psycopg2
Storing debug log for failure in /Users/Tyler/Library/Logs/pip.log

运行easy_install或从源代码执行都会在最后给出相同的错误(关于-lssl的库未找到的部分)


运行brew安装(或升级)openssl会产生以下结果

$ brew upgrade openssl
Error: openssl-1.0.1h already installed

有人能帮我吗


Tags: installpipvarwitherrorrecordbzpsycopg2
3条回答

对于在macOS Sierra 10.12(或更高版本,最有可能)上寻找此解决方案的任何人:我通过安装命令行工具修复了此问题:

xcode-select --install

在那之后,pip install psycopg2应该会起作用

如果没有,您还可以尝试链接brew的openssl:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2

通过brew安装openssl。请注意brew link openssl --force不再工作:

$ brew link openssl --force                                                                                 17.5s
Warning: Refusing to link: openssl
Linking keg-only openssl means you may end up linking against the insecure,
deprecated system OpenSSL while using the headers from Homebrew's openssl.
Instead, pass the full include/library paths to your compiler e.g.:
  -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib

正如@macho在下面指出的,如果这仍然不起作用,您可能需要使用pip的--no-cache选项,例如

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip --no-cache install psycopg2

记住,如果您构建在ARM/Apple M1 Mac上,请相应地调整这些路径(因为homebrew安装在/opt/homebrew/);命令如下:

env LDFLAGS="-I/opt/homebrew/opt/openssl/include -L/opt/homebrew/opt/openssl/lib" pip --no-cache install psycopg2

我从brew安装了OpenSSL(brew install openssl

以下几点对我很有用:

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

使用brew link openssl是危险的,因为它可能会通过将Homebrew的OpenSSL头进行符号链接而弄乱您的系统,而实际的库仍然是系统提供的库,从而导致各种问题。如果您尝试,Homebrew实际上会警告您不要这样做(其他答案表明链接无论如何也解决不了问题):

$ brew link openssl
Warning: Refusing to link: openssl
Linking keg-only openssl means you may end up linking against the insecure,
deprecated system OpenSSL while using the headers from Homebrew's openssl.
Instead, pass the full include/library paths to your compiler e.g.:
  -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib

以下是您需要使用的pip命令:

$ pip install -r requirements.txt --global-option=build_ext --global-option="-I/usr/local/opt/openssl/include" --global-option="-L/usr/local/opt/openssl/lib"

对于pipenv,我不知道您可以传递给它的任何命令行属性,但是您可以在运行pipenv install之前将上述路径导出为环境变量:

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

相关问题 更多 >