Python / Pip 国内镜像源配置与报错处理
直连国外源装 Python 包或者下大模型动不动就超时断连,这里记一下平时常用的 Pip 换源命令、信任主机报错解法、PyTorch 版本对应关系,以及 HuggingFace 国内镜像下载方法。
Pip 换阿里云源
需要查其他镜像站可以直接去 阿里云镜像站 右上角搜索。设置全局 Pip 镜像:
sh
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/如果用的是 http:// 地址,安装时经常会报下面这个“不信任主机”警告并跳过下载:
sh
WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored.解决办法很简单:把地址改成 https://,或者在 pip install 后面临时加上 --trusted-host mirrors.aliyun.com。
python、cuda、pytorch之间的版本约束
以python3.9和cuda11.8对应的torch2.0.0版本为例
linux的名称为torch-2.0.0+cu118-cp39-cp39-linux_x86_64.whl
torch-2.0.0指torch版本号cp39指python3.9linux版本名称带有linux
同理,windows的版本名称为torch-2.0.0+cu118-cp39-cp39-win_amd64.whl
| PyTorch版本 | 对应的Python |
|---|---|
| PyTorch1.0 | Python 2.7,3.5,3.6,3.7 |
| PyTorch1.1 | Python 2.7,3.5,3.6,3.7 |
| PyTorch1.2 | Python 2.7,3.5,3.6,3.7 |
| PyTorch1.3 | Python 2.7,3.5,3.6,3.7 |
| PyTorch1.4 | Python 2.7,3.5,3.6,3.7,3.8 |
| PyTorch1.5 | Python 3.5,3.6,3.7,3.8 |
| PyTorch1.6 | Python 3.5,3.6,3.7,3.8 |
| PyTorch1.7 | Python 3.5,3.6,3.7,3.8 |
| PyTorch1.8 | Python 3.6,3.7,3.8,3.9 |
| PyTorch1.9 | Python 3.6,3.7,3.8,3.9 |
| PyTorch2.0 | Python 3.8,3.9,3.10 |
| PyTorch2.1 | Python 3.8,3.9,3.10,3.11 |
| PyTorch2.2 | Python 3.8,3.9,3.10,3.11 |
| PyTorch2.3 | Python 3.10,3.11,3.12 |
| PyTorch2.4 | Python 3.10,3.11,3.12 |
| PyTorch2.5 | Python 3.9,3.10,3.11,3.12,3.13 |
python包国内下不了怎么办?
.condarc文件地址--C:\Users\用户名
.condarc文件解释--以添加清华镜像源为例
sh
# 这是 condarc 文件中用于指定 Conda 包来源通道的部分。它定义了 Conda 在查找和安装包时会优先使用的通道列表。
channels:
# 这是 Conda 默认的通道,通常指向 Anaconda 官方提供的包仓库。它包含了大部分常用的 Python 包和依赖项。
- defaults
# 这个选项用于控制 Conda 是否在输出中显示包的来源通道 URL。
# 当设置为 true 时,Conda 在安装包或列出包信息时会显示包所在的通道 URL。这有助于用户了解包的来源,便于调试和管理
show_channel_urls: true
# 这是 Conda 默认的通道列表,通常由 Anaconda 官方维护。这些通道是 Conda 在没有其他指定通道时会使用的默认包来源。
default_channels:
# 这是清华大学提供的 Anaconda 镜像,包含主通道的包。
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
# 这是针对 R 语言包的通道。
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
# 这是针对 MSYS2(一个用于 Windows 的 Unix-like 环境)的包通道。
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
# 这是用户自定义的通道配置,通常用于指定特定的包来源,比如社区维护的通道或第三方提供的通道。
custom_channels:
# 这是一个由社区维护的通道,提供大量的高质量包。这里将其指向了清华大学的镜像。
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
# 这是 PyTorch 官方的通道,用于获取 PyTorch 相关的包。同样指向了清华大学的镜像。
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
# 指定虚拟环境路径
envs_dirs:
- D:/Conda/envs
# 指定包环境路径
pkgs_dirs:
- D:/Conda/pkgs快捷复制版本
sh
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud清除缓存
sh
conda clean -iHuggingFace 国内镜像下载(hf-mirror)
使用 diffusers 或下载大模型权重时,可通过 huggingface-cli 配合国内镜像 hf-mirror 断点续传,避免直连中断。
- 安装依赖:
sh
pip install -U huggingface_hub- 设置镜像环境变量:
- Linux / macOS(可写入
~/.bashrc):
sh
export HF_ENDPOINT=https://hf-mirror.com- Windows PowerShell:
powershell
$env:HF_ENDPOINT = "https://hf-mirror.com"- 登录并下载模型(如需访问受限仓库先执行
login):
sh
huggingface-cli login --token <你的HF_Token>
huggingface-cli download <仓库名,如 black-forest-labs/FLUX.1-dev> --local-dir ./downloaded_repo