更换Python版本后pip无法下载包的网络问题求助(Ubuntu)
问题现象
切换Python版本后,pip安装包时出现网络连接错误,无法访问files.pythonhosted.org,核心报错如下:
ERROR: Could not install packages due to an OSError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/f5/11/fc3de994aa4648632f3512ca18686fb82e6f250d82dda544a09f96726dae/pipenv-2023.11.15-py3-none-any.whl.metadata (Caused by NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f23739c9d60>: Failed to establish a new connection: [Errno 101] Network is unreachable'))
执行pip install时还会出现权限提示+重试报错:
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f7b37313f40>: Failed to establish a new connection: [Errno 101] Network is unreachable')':
环境信息
- 系统:Ubuntu 18.04.1 / 20.04.4
- Python版本:3.8 / 3.12
- pip版本:23.3.1
- 触发节点:执行
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8切换Python版本后出现问题,此前Python3.6环境下pip工作正常
已尝试的无效操作
- 网络连通性测试:
ping google.com、ping pypi.org均正常,pypi.org ping结果:
PING pypi.org (151.101.64.223) 56(84) bytes of data. 64 bytes from 151.101.64.223 (151.101.64.223): icmp_seq=1 ttl=54 time=6.10 ms 64 bytes from 151.101.64.223 (151.101.64.223): icmp_seq=2 ttl=54 time=6.12 ms 64 bytes from 151.101.64.223 (151.101.64.223): icmp_seq=3 ttl=54 time=6.09 ms 64 bytes from 151.101.64.223 (151.101.64.223): icmp_seq=4 ttl=54 time=6.14 ms 64 bytes from 151.101.64.223 (151.101.64.223): icmp_seq=5 ttl=54 time=6.11 ms 64 bytes from 151.101.64.223 (151.101.64.223): icmp_seq=6 ttl=54 time=6.09 ms ^C --- pypi.org ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 5008ms
- 重装pip和Python,执行
apt-get update,切换至Python2,移除Python alternatives配置 - 卸载Anaconda Python
- 创建软链接:
ln -s /usr/bin/python3 /usr/bin/python3.8 - 安装其他Python版本(3.7、3.9、3.10、3.11等)
- 测试curl可正常访问
https://pypi.org/simple/flask-bcrypt/ - 执行命令:
pip install --upgrade pip --trusted-host pypi.org --trusted-host files.pythonhosted.org
可行解决方案
1. 清除pip代理配置
pip可能继承了无效的代理设置,导致连接失败。执行以下命令清除:
pip config unset global.proxy pip config unset install.proxy
若存在系统级配置,检查/etc/pip.conf文件,删除其中代理相关行。
2. 切换国内PyPI镜像源
绕过files.pythonhosted.org的访问问题,使用国内镜像源:
- 临时使用(以清华镜像为例):
pip install pipenv -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
- 永久配置:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
3. 修复Python与pip的版本绑定
切换Python版本后,全局pip可能未关联到目标版本,导致异常。直接使用对应Python版本的pip:
# 以Python3.8为例 python3.8 -m pip install --upgrade pip # 后续安装包使用 python3.8 -m pip install <package-name>
4. 检查防火墙/iptables规则
部分防火墙规则可能限制Python进程的HTTPS出站连接:
- 查看iptables规则:
sudo iptables -L -n
- 临时关闭防火墙测试:
sudo ufw disable
测试正常后,重新配置防火墙允许443端口出站。
5. 修复DNS解析
files.pythonhosted.org可能存在DNS解析异常:
- 手动解析域名:
nslookup files.pythonhosted.org
- 更换系统DNS服务器(编辑
/etc/resolv.conf):
sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf sudo echo "nameserver 114.114.114.114" >> /etc/resolv.conf
内容的提问来源于stack exchange,提问作者jihyeon

