配置 pip 镜像源
要配置 pip
使用国内的镜像源以加速包的下载,可以按照以下步骤操作:
1. 临时使用镜像源
在安装包时,可以通过 -i
参数临时指定镜像源。例如:
pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple
常用的国内镜像源有:
- 清华大学:
https://pypi.tuna.tsinghua.edu.cn/simple
- 阿里云:
https://mirrors.aliyun.com/pypi/simple/
- 豆瓣:
https://pypi.douban.com/simple/
- 中国科技大学:
https://pypi.mirrors.ustc.edu.cn/simple/
2. 永久配置镜像源
可以通过修改 pip
的配置文件来永久使用镜像源。
方法一:使用命令行配置
运行以下命令来设置默认的镜像源:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
方法二:手动编辑配置文件
-
找到或创建
pip
的配置文件:- Linux/macOS:
~/.pip/pip.conf
或~/.config/pip/pip.conf
- Windows:
%APPDATA%\pip\pip.ini
- Linux/macOS:
-
在配置文件中添加以下内容:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple
3. 验证配置
可以通过以下命令验证配置是否生效:
pip config list
如果看到 index-url
已经设置为指定的镜像源,说明配置成功。
4. 恢复默认源
如果需要恢复默认的官方源,可以删除配置文件中的 index-url
配置,或者运行以下命令:
pip config unset global.index-url
这样 pip
就会重新使用默认的官方源。