配置 pip 镜像源

配置 pip 镜像源

要配置 pip 使用国内的镜像源以加速包的下载,可以按照以下步骤操作:

1. 临时使用镜像源

在安装包时,可以通过 -i 参数临时指定镜像源。例如:

pip install 包名 -i https://mirrors.aliyun.com/pypi/simple/

常用的国内镜像源有:

  • 阿里云:https://mirrors.aliyun.com/pypi/simple/
  • 中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
  • 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple

2. 永久配置镜像源

可以通过修改 pip 的配置文件来永久使用镜像源。

方法一:使用命令行配置

运行以下命令来设置默认的镜像源:

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

方法二:手动编辑配置文件

  1. 找到或创建 pip 的配置文件:

    • Linux/macOS: ~/.pip/pip.conf~/.config/pip/pip.conf
    • Windows: %APPDATA%\pip\pip.ini
  2. 在配置文件中添加以下内容:

    [global]
    index-url = https://mirrors.aliyun.com/pypi/simple/
    

3. 验证配置

可以通过以下命令验证配置是否生效:

pip config list

如果看到 index-url 已经设置为指定的镜像源,说明配置成功。

4. 恢复默认源

如果需要恢复默认的官方源,可以删除配置文件中的 index-url 配置,或者运行以下命令:

pip config unset global.index-url

这样 pip 就会重新使用默认的官方源。

posted @ 2025-03-18 09:42  飞仔FeiZai  阅读(1051)  评论(0)    收藏  举报