Python中提高pip install速度

程一诺
程一诺
发布时间:2026-10-13| 6人浏览过
原创

针对pip install默认官方源连接慢或超时的问题,本文介绍了使用国内镜像源加速下载的方法。内容涵盖临时使用参数指定源、Windows/Linux永久配置文件设置,以及提供阿里云、中科大等常用镜像地址,并附带自动配置批处理脚本,帮助用户提升模块安装效率。

pip安装速度慢的问题与解决思路

执行pip install命令时,系统默认连接Python官方源。受网络环境影响,连接速度往往非常缓慢,甚至出现超时中断的情况,导致大量模块无法安装,令人困扰。

该如何解决这一难题?

建议采用国内镜像源。将下方提供的命令完整复制,粘贴至记事本中,保存为bat批处理文件,双击运行该文件即可完成配置。

一键配置批处理脚本


@echo off

rem 声明采用UTF-8编码chcp 65001

echo \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

echo 自动设置pip安装配置文件

echo \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

set pipFolder=\\pip\\

if exist %userprofile%%pipFolder% (

echo 目录%userprofile%%pipFolder%已存在无需创建

) else (

echo 创建%userprofile%%pipFolder%

md %userprofile%%PiPFolder%

)

cd %userprofile%%pipFolder%

echo \[global\]>pip.ini

(

echo timeout=60

echo index-url=http://pypi.douban.com/simple

echo extra-index-url=https://pypi.tuna.tsinghua.edu.cn/simple/

echo \[install\]

echo trusted-host=pypi.douban.com

echo pypi.tuna.tsinghua.edu.cn

echo timeout = 10

)>>pip.ini

wmic ENVIRONMENT where "name='path' and username='<system>'"

set VariableValue="%path%;%userprofile%%pipFolder%pip.ini"

配置完成后,再次尝试pip install命令,体验将显著提升,下载速度如飞。

常用国内镜像源列表

此外,整理了一些常用的pip国内镜像源供参考:

阿里云 https://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(douban) https://pypi.douban.com/simple/ 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/

镜像源配置方法

具体使用方法如下:

临时生效方式 在命令中直接添加参数 -i 指定镜像源。

示例如下:

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

永久生效方式

1. Linux系统下,需修改 ~/.pip/pip.conf 文件(若不存在,则创建相应文件夹及文件。注意文件夹名需以“.”开头,表示隐藏文件夹)。

配置文件内容如下:

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

2. Windows系统下,直接在用户目录中创建pip文件夹(例如:C:\Users\xx\pip),并在其中新建文件pip.ini。文件内容与上述Linux配置相同即可。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。