我可以使用“pip install --cert”吗?

Ian*_*Lee 29 linux certificate ssl linux-mint ubuntu

试图弄清楚是否有一种方法可以避免在我在工作中安装软件包时使用 --cert 标志进行 pip。代理存在一些问题,尽管将mycert.crt文件添加到/usr/local/share/ca-certificates并运行sudo update-ca-certificates.

我看到的消息的一个例子是:

$ pip install "virtualenv>=1.10.1"
Downloading/unpacking virtualenv>=1.10.1
  Could not fetch URL https://pypi.python.org/simple/virtualenv/: There was a problem confirming the ssl certificate: <urlopen error [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed>
  Will skip URL https://pypi.python.org/simple/virtualenv/ when looking for download links for virtualenv>=1.10.1
  Could not fetch URL https://pypi.python.org/simple/: There was a problem confirming the ssl certificate: <urlopen error [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed>
  Will skip URL https://pypi.python.org/simple/ when looking for download links for virtualenv>=1.10.1
  Cannot fetch index base URL https://pypi.python.org/simple/
  Could not fetch URL https://pypi.python.org/simple/virtualenv/: There was a problem confirming the ssl certificate: <urlopen error [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed>
  Will skip URL https://pypi.python.org/simple/virtualenv/ when looking for download links for virtualenv>=1.10.1
  Could not find any downloads that satisfy the requirement virtualenv>=1.10.1
Cleaning up...
No distributions at all found for virtualenv>=1.10.1
Storing complete log in /tmp/tmpwW5qXD
Run Code Online (Sandbox Code Playgroud)

这可以通过使用来解决:

pip install --cert=/usr/local/share/ca-certificates/mycert.crt
Run Code Online (Sandbox Code Playgroud)

但是,我宁愿不必这样做(因为我确信其他应用程序会遇到此问题)。

我正在运行 Linux Mint 15(尽管我之前在 Ubuntu 12.04 上遇到了非常相似的问题),pip 版本 1.4.1。

小智 14

您可以通过 pip 配置文件进行设置,该文件位于 Windows中$HOME/.pip/pip.conf%APPDATA%\pip\pip.iniWindows 上:

[global]
cert = /usr/local/share/ca-certificate/mycert.crt
Run Code Online (Sandbox Code Playgroud)

这个文件让你基本上可以设置 pip 使用的所有标志。完整文档位于https://pip.pypa.io/en/latest/user_guide/#configuration

  • 我认为您对我的原始帖子的评论会得出真正的答案,即 pip 不使用系统证书。这是一个很好的解决方案,尽管当我在工作环境和家庭环境(通过 VPN)之间切换时会遇到问题。证书的问题比解决方案更重要。 (3认同)
  • 感谢@IanLee 的提示。以下是支持文档:“从 v1.3 开始,pip 通过 HTTP 提供 SSL 证书验证,以防止针对 PyPI 下载的中间人攻击。这不使用系统证书存储,而是使用捆绑的 CA 证书存储. 可以使用 --cert 选项或使用 PIP_CERT、REQUESTS_CA_BUNDLE 或 CURL_CA_BUNDLE 环境变量来覆盖默认的捆绑 CA 证书存储证书存储。 https://pip.pypa.io/en/latest/cli/pip_install/?highlight=SSL%20Certificate%20Verification#ssl-certificate-verification (3认同)

小智 8

这对我有用,不需要知道配置文件所在的位置:

python -m pip config set global.cert C:\\Path\\cert.crt
Run Code Online (Sandbox Code Playgroud)

我相信您需要 pip 版本 10+,您可以通过以下方式找到它:

 python -m pip --version
Run Code Online (Sandbox Code Playgroud)

config set 命令的输出随后会输出配置文件的名称,以方便您使用


and*_*zep 5

对我来说,没有任何配置文件解决方法有效。我在Ubuntu 14.04上使用pip 1.5.4

最终对我有用的是首先在系统上安装证书(对我来说在 ubuntu 上就是这样)

sudo cp ~/my_cert.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates
Run Code Online (Sandbox Code Playgroud)

以前的自动更新捆绑文件(检查底部/etc/ssl/certs/ca-certificates.crt现在应该看到与 中相同的证书my_cert.crt

现在在PIP_CERT. 并将其添加到我的.bashrc

echo export PIP_CERT=/etc/ssl/certs/ca-certificates.crt >> ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

免责声明:我已经在 SO 中发布了这个答案(与上面“最终重复的链接”中的答案相同,但一开始我没有找到另一个(最终重复的答案)......所以如果像我这样的人先到这里,那么这可能会有所帮助。

也许我违反了某种规则来发布相同的答案两次,一个在 SO 中,另一个在超级用户中。如果是这样,对不起。