pip配置其他镜像源
时间: 2025-03-11 16:17:25 浏览: 32
### 配置 pip 使用国内或其他第三方镜像源
为了提高 Python 包的下载速度,可以通过配置 `pip` 来使用更快捷的国内或第三方镜像源。具体操作方式有多种:
#### 方法一:临时指定镜像源
可以在每次执行 `pip install` 命令时手动添加参数 `-i` 或者 `--index-url` 后面跟上所需使用的镜像地址。
例如要安装 requests 库并使用清华大学的镜像,则可运行如下命令:
```bash
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple/
```
#### 方法二:设置环境变量
对于 Windows 用户来说,在系统的高级属性里新建名为 `PIP_INDEX_URL` 的环境变量,并将其值设为想要使用的镜像链接;而对于 Linux 和 macOS 用户而言,则可以直接编辑 shell profile 文件(如 `.bashrc`,`.zshrc`),加入下面这行代码:
```bash
export PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple/
```
之后记得使更改生效:
```bash
source ~/.bashrc # 如果是 bash 终端的话
```
#### 方法三:修改全局配置文件
找到位于用户目录下的 `~/.pip/pip.conf`(Linux/macOS)或者 `%APPDATA%\pip\pip.ini `(Windows)中的配置文件,如果没有该文件则创建它。接着向其中写入以下内容以永久改变默认索引服务器:
```ini
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
```
以上三种方法都可以有效地帮助解决由于网络原因造成的包安装缓慢问题[^1]。
#### 推荐的国内镜像源列表
- 豆瓣镜像源:<http://pypi.douban.com/simple/>
- 清华大学镜像源:<https://pypi.tuna.tsinghua.edu.cn/simple/>
- 阿里云镜像源:<http://mirrors.aliyun.com/pypi/simple/>
阅读全文
相关推荐


















