Python pip更换镜像源问题及解决
作者:T-I-M
文章介绍了Python的pip包管理工具更换镜像源的方法,包括清华大学、阿里云、中国科技大学和豆瓣等常用镜像源,文章详细说明了临时使用镜像源和永久配置镜像源的方法,并附上了针对Conda的镜像配置示例,最后,作者推荐使用国内镜像源以提高下载速度
Python pip更换镜像源问题
常用的镜像源:
- 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
- 阿里云:https://mirrors.aliyun.com/pypi/simple/
- 中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
1. 临时使用镜像源
如果只是偶尔需要使用镜像源,可以在安装包时通过 -i
参数指定。
例如:
pip install <package_name> -i https://pypi.tuna.tsinghua.edu.cn/simple
这里的 -i
参数后面跟着的是镜像源的URL。
例如:
安装numpy:
# 清华镜像 pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple # 阿里云镜像 pip install numpy -i https://mirrors.aliyun.com/pypi/simple/
2. 永久配置镜像源
为了避免每次安装包时都手动指定镜像源,可以通过配置文件永久设置镜像源。
例如:
# 永久配置阿里云镜像源 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
验证配置是否生效:
运行以下命令查看当前配置:
pip config list
3. 附:针对 Conda 的镜像配置
如果你使用 conda
,可编辑 ~/.condarc
(Linux/macOS)或 C:\Users\<用户名>\.condarc
(Windows),添加以下内容(以清华镜像为例):
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
总结
- 临时使用:直接在命令中添加
-i <镜像源地址>
。 - 永久生效:修改
pip
或conda
的配置文件。 - 推荐使用清华大学或阿里云等国内镜像源,速度更快。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。