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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26288042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:20:03  来源:igfitidea点击:

error installing psycopg2, library not found for -lssl

pythonpostgresqlpsycopg2

提问by tscizzle

I run

我跑

sudo pip install psycopg2

and I get a bunch of output that looks like:

我得到了一堆看起来像的输出:

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

And at the end it says:

最后它说:

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


Running easy_installor doing it from sourceboth give me the same error at the end (the part about library not found for -lssl).

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



Running brew install (or upgrade) openssl yields the below

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

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

Can anyone help me out?

谁能帮我吗?

回答by ecordo

I've managed to fix it by using:

我设法使用以下方法修复它:

brew unlink openssl && brew link openssl --force

I am not sure how this differs from the brew uninstall/upgrades that I did on OpenSSL in prior attempts I've made. My assumption is that these operations left some of the "faulty" shared libraries which were preventing this from working. Note that this also fixed issues with installing python cryptography module.

我不确定这与我之前在 OpenSSL 上所做的 brew 卸载/升级有何不同。我的假设是这些操作留下了一些“错误”的共享库,这些库阻止了它的工作。请注意,这也解决了安装 python 加密模块的问题。

回答by sas

For anyone looking for a solution for this on macOS Sierra 10.12 (or later, most likely): I fixed this by installing the command line tools:

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

xcode-select --install

After that, pip install psycopg2should work.

之后,pip install psycopg2应该工作。

If it doesn't, you could also try to link against brew's openssl:

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

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

with openssl installed via brew. Note that the brew link openssl --forcedoes not work anymore:

通过 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

As @macho points out below if this still does not work, you might need to use the --no-cacheoption of pip, e.g.

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

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

回答by André Borie

When running brew link opensslI get the following message:

运行时,brew link openssl我收到以下消息:

$ 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

Following this advice here's the pipcommand you need to use:

遵循此建议,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"

回答by dixon1e

Recently had this problem in High Sierra, having just installed Python 3.7 in a virtualenv.

最近在 High Sierra 中遇到了这个问题,刚刚在 virtualenv 中安装了 Python 3.7。

The solution is to use a later version of psycopg2. Version 2.7.7 worked, where 2.7.1 did not.

解决方案是使用更高版本的 psycopg2。2.7.7 版有效,而 2.7.1 版无效。

回答by Isaac Philip

What worked for me was the hint provided in the command to link openssl,

对我有用的是链接openssl的命令中提供的提示,

$ brew link openssl
Warning: Refusing to link macOS-provided software: openssl
If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

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

$ pip install psycopg2
Collecting psycopg2
  Using cached https://files.pythonhosted.org/packages/23/7e/93c325482c328619870b6cd09370f6dbe1148283daca65115cd63642e60f/psycopg2-2.8.2.tar.gz
Installing collected packages: psycopg2
  Running setup.py install for psycopg2 ... done
Successfully installed psycopg2-2.8.2

回答by IamAshay

I had OpenSSL installed from brew (brew install openssl)

我从 brew ( brew install openssl)安装了 OpenSSL

The following worked for me:

以下对我有用:

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

回答by Flimm

Instead of installing psycopg2, install psycopg2-binary, from the same authors:

而不是安装psycopg2, install psycopg2-binary,来自同一作者:

pip install psycopg2-binary

This is what the documentation says about this PyPI package:

这是文档中关于这个 PyPI 包的内容:

You can [...] obtain a stand-alone package, not requiring a compiler or external libraries, by installing the psycopg2-binary package from PyPI:

$ pip install psycopg2-binary

The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources.

通过从 PyPI 安装 psycopg2-binary 包,您可以 [...] 获得一个独立的包,不需要编译器或外部库:

$ pip install psycopg2-binary

二进制包是开发和测试的实用选择,但在生产中建议使用从源代码构建的包。

回答by pmneve

On mojave I added these to the .bash_profile

在 mojave 上,我将这些添加到 .bash_profile

export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/curl/lib -L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/curl/include -I/user/local/opt/openssl/include"

was then able to install psycopg 2.8.3 in a python 3.7.4 virtualenv.

然后能够在 python 3.7.4 virtualenv 中安装 psycopg 2.8.3。

This after reinstalling xcode and the command line tools.

这是在重新安装 xcode 和命令行工具之后。

All the answers above helped!

以上所有答案都有帮助!

回答by Yngve H?iseth

Using Fish, the following two commands solved this issue for me after installing OpenSSL with Homebrew.

使用 Fish,在使用 Homebrew 安装 OpenSSL 后,以下两个命令为我解决了这个问题。

set -gx LDFLAGS "-L/usr/local/opt/openssl/lib"
set -gx CPPFLAGS "-I/usr/local/opt/openssl/include"

Use brew info opensslto get up-to-date info.

使用brew info openssl之后,可以获取最新信息。

回答by ProfX

I was having this issue on Mojave. Mojave does not create a /usr/include directory, which psycopg2 needs to install. This was not obvious. I found the solution here: How to update Xcode from command line, which references: https://forums.developer.apple.com/thread/104296

我在 Mojave 上遇到了这个问题。Mojave 不会创建 /usr/include 目录,psycopg2 需要安装该目录。这并不明显。我在这里找到了解决方案: How to update Xcode from command line,参考:https: //forums.developer.apple.com/thread/104296