python@pip安装源http/https区别

在使用 pip 安装 Python 包时,如果你遇到 SSL 证书验证错误,或者要从一个 自定义/不受信的源 安装包,pip 提供了 --trusted-host 参数,允许你显式信任某个主机

在https下则不需要显式信任主机


✅ 1.基本用法

1
pip install <包名> --trusted-host <主机名>

🔧 示例:

假设你从 http://pypi.mycompany.com/simple 安装包,且该主机没有有效的 HTTPS 证书:

1
pip install somepackage --index-url http://pypi.mycompany.com/simple --trusted-host pypi.mycompany.com

💡 2.常见场景

1. 从 HTTP 源安装(非 HTTPS)

1
pip install somepackage --index-url http://example.com/simple --trusted-host example.com

2. 解决 SSL 证书错误

错误提示示例:

There was a problem confirming the ssl certificate: [SSL: CERTIFICATE_VERIFY_FAILED]

解决方案:

1
pip install somepackage --trusted-host pypi.org --trusted-host files.pythonhosted.org

✅ 多个 trusted-host 的写法

如果需要信任多个域名,可以加多个 --trusted-host

1
2
3
pip install somepackage \
--trusted-host pypi.org \
--trusted-host files.pythonhosted.org

📁 3.永久配置(可选)

可以将信任的主机配置到 pip.ini(Windows)或 pip.conf(Linux/macOS)中:

Windows:

路径:%APPDATA%\pip\pip.ini

1
2
3
[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com

Linux/macOS:

路径:~/.config/pip/pip.conf~/.pip/pip.conf

1
2
3
[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com