前言
众所周知,从官方默认的 pip 源安装 python 包经常会遇到网络问题导致下载缓慢甚至无法下载。如果使用镜像源安装又存在更新不及时的问题,会遇到如下的报错:
uv -v pip install "sglang[all]>=0.4.6.post2" -i https://mirrors.cloud.tencent.com/pypi/simple
DEBUG Found fresh response for: https://mirrors.cloud.tencent.com/pypi/simple/sglang/
DEBUG Searching for a compatible version of sglang[all] (>=0.4.6.post2)
DEBUG No compatible version found for: sglang[all]
× No solution found when resolving dependencies:
╰─▶ Because only sglang[all]<=0.4.6.post1 is available and you require sglang[all]>=0.4.6.post2, we can conclude that your requirements are unsatisfiable.
找一台国外线路好的 VPS,使用 nginx 反代官方 pip 源可以一次性解决上面所有问题。
安装 nginx
请参考官方文档完成 nginx 安装,不再赘述。
反代配置
进入 nginx 配置 /etc/nginx/conf.d
目录,新增一个 nginx 配置。请将 pypi.example.com
替换为实际的域名,并建议参考网上其他教程开启 SSL,
server {
listen 80 http2;
server_name pypi.example.com;
proxy_http_version 1.1;
proxy_read_timeout 600s;
proxy_connect_timeout 30s;
proxy_buffering off;
location / {
return 403;
}
location /simple {
proxy_pass https://pypi.org;
proxy_set_header Accept-Encoding "";
proxy_ssl_server_name on;
proxy_ssl_name pypi.org;
sub_filter 'https://files.pythonhosted.org' 'http://pypi.example.com';
sub_filter_once off;
sub_filter_types *;
}
location /packages {
proxy_pass https://files.pythonhosted.org;
proxy_set_header Accept-Encoding "";
proxy_ssl_server_name on;
proxy_ssl_name files.pythonhosted.org;
}
}
然后执行 systemctl reload nginx
即可。
测试
使用 pip install
命令指定 --index-url
为反代 url,如果 pip
日志显示 whl 包下载 url 为反代 url,即表示搭建成功。
pip install -v numpy -i http://pypi.example.com/simple --trusted-host pypi.example.com