问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

高效配置pip,用清华大学镜像加速代码部署

创作时间:
作者:
@小白创作中心

高效配置pip,用清华大学镜像加速代码部署

引用
CSDN
9
来源
1.
https://blog.csdn.net/weixin_43416960/article/details/137209773
2.
https://blog.csdn.net/qq_40703755/article/details/141872194
3.
https://blog.csdn.net/qq_33356563/article/details/90509973
4.
https://blog.csdn.net/qq_42206726/article/details/137414100
5.
https://blog.csdn.net/duoyasong5907/article/details/129190001
6.
https://zhuanlan.zhihu.com/p/605971379
7.
https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
8.
http://www.runoob.com/w3cnote/pip-cn-mirror.html
9.
https://www.cnblogs.com/sx66/p/17824023.html

在Python开发过程中,频繁使用pip管理项目依赖时常常会遇到安装速度慢的问题。本文将介绍如何通过配置清华大学镜像源来显著提升pip的下载速度,从而加快代码部署过程。通过简单的步骤,在pip配置文件中添加清华大学镜像源地址,可以有效解决因网络延迟导致的包下载缓慢问题,提高开发效率。

1. 配置全局镜像源

Windows系统

  1. 创建配置文件

    在用户目录下创建pip文件夹和pip.ini文件。具体路径为:

    C:\Users\你的用户名\pip\pip.ini
    

    如果pip文件夹不存在,请手动创建。

  2. 写入配置内容

    用记事本打开pip.ini,输入以下内容并保存:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    trusted-host = pypi.tuna.tsinghua.edu.cn
    

Linux/macOS系统

  1. 创建配置文件

    在用户目录下创建.pip文件夹和pip.conf文件:

    mkdir -p ~/.pip
    touch ~/.pip/pip.conf
    
  2. 写入配置内容

    用文本编辑器打开~/.pip/pip.conf,输入以下内容并保存:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    trusted-host = pypi.tuna.tsinghua.edu.cn
    

2. 验证配置是否生效

  • 执行以下命令查看当前pip配置:

    pip config list
    

    正常输出应包含:

    global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
    global.trusted-host='pypi.tuna.tsinghua.edu.cn'
    
  • 测试安装包(观察下载源是否为清华镜像):

    pip install numpy  # 任意安装一个包测试
    

3. 虚拟环境配置

如果使用虚拟环境,需要单独配置镜像源:

  1. 激活虚拟环境

    # Linux/macOS
    source myenv/bin/activate
    
    # Windows
    myenv\Scripts\activate
    
  2. 创建或编辑配置文件

    • Linux/macOS: myenv/pip.conf
    • Windows: myenv\pip.ini
  3. 添加清华镜像源

    在上述文件中添加:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    

4. 其他常用镜像源

除了清华大学镜像源,还有多个国内镜像源可供选择:

  • 阿里云:http://mirrors.aliyun.com/pypi/simple/
  • 中国科技大学:http://pypi.mirrors.ustc.edu.cn/simple/
  • 豆瓣:http://pypi.douban.com/simple/

5. 配置多个镜像源

可以在配置文件中添加多个镜像源:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
extra-index-url =
    https://mirrors.aliyun.com/pypi/simple/
    https://mirror.baidu.com/pypi/simple

通过以上步骤,所有pip install命令将默认使用清华镜像源加速下载。如果遇到任何问题,可以检查配置文件路径是否正确、格式是否规范,以及镜像源地址是否准确。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号