由于 OSError 无法安装软件包:[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:自签名证书 (_ssl.c:1131)

Jon*_*ios 0 python ssl cacerts

我一直试图通过阅读这里的不同答案来解决这个问题,但没有一个答案被证明是“解决方案”,所以我会尝试简要解释我的情况,以便你们可以给我一个线索。问题是,当我尝试运行pip install <package>它时,会出现以下警告(只是为了向您展示,使用 fastapi 作为示例):

  WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1131)'))': /packages/4f/46/226355e82ccb4be82e06269e9a546f16c1d87fbda2286fb5d36a1c31af9e/fastapi-0.75.0-py3-none-any.whl
  WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1131)'))': /packages/4f/46/226355e82ccb4be82e06269e9a546f16c1d87fbda2286fb5d36a1c31af9e/fastapi-0.75.0-py3-none-any.whl
  WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1131)'))': /packages/4f/46/226355e82ccb4be82e06269e9a546f16c1d87fbda2286fb5d36a1c31af9e/fastapi-0.75.0-py3-none-any.whl
  WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1131)'))': /packages/4f/46/226355e82ccb4be82e06269e9a546f16c1d87fbda2286fb5d36a1c31af9e/fastapi-0.75.0-py3-none-any.whl
Run Code Online (Sandbox Code Playgroud)

结尾是:

ERROR: Could not install packages due to an OSError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/4f/46/226355e82ccb4be82e06269e9a546f16c1d87fbda2286fb5d36a1c31af9e/fastapi-0.75.0-py3-none-any.whl (Caused by SSLError(SSLCertVerificationError(1,'[SSL:CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate(_ssl.c:1131)')))
Run Code Online (Sandbox Code Playgroud)

我尝试的第一件事是pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org <package>,但它只给了我另一个错误。所以我开始阅读有关 SSL 证书的内容,当错误显示自签名证书时,这意味着我尝试连接的服务器,他的证书是由服务器本身自签名的,或者提供链签名的实体不是在浏览器的 白名单中。而事实确实如此!

但是当我使用一些在线工具检查该网站的证书时,一切似乎都是可靠的

语境:

  • 我上次下载软件包是大约一个月前(我昨天才意识到这一点)
  • Python版本:3.8.10 / pip版本:21.1.1
  • Windows 10 /个人计算机和家庭网络(无公司权限)
  • 这种情况发生在每个级别:当尝试构建 docker 时,在 a 内部venv以及系统级别(venv 外部)

所以,我真的不知道从哪里开始。我的本地机器有问题吗?,pythonhosted 是问题吗?,我是否把事情搞混了?,我应该卸载/重新安装 Python 吗?

小智 6

下面的命令应该可以解决您的问题。

pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org 
<package-name>
Run Code Online (Sandbox Code Playgroud)

例如:

pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org 
pandas
Run Code Online (Sandbox Code Playgroud)