Python pip 命令完全指南:从入门到精通(2026最新版) 🐍
大家好!pip 是 Python 的官方包管理工具(Python Package Installer),从 Python 3.4 开始默认内置。它负责从 PyPI(Python Package Index)安装、升级、卸载和管理第三方包,是每个 Python 开发者每天都要用的工具。
本文基于 pip 26.0.1(2026年2月发布),覆盖基础 → 进阶 → 生产最佳实践,全部命令均可直接复制运行。建议始终使用 python -m pip 方式,避免多版本冲突。
1. pip 基础检查与升级(第一步必做!)
# 查看 pip 版本(推荐方式)
python -m pip --version
# 升级 pip 到最新版(强烈建议每次都跑)
python -m pip install --upgrade pip
# 查看帮助
python -m pip --help
2026最新变化:pip 26.0 新增 --all-releases、--only-final(精细控制预发布版)和 --uploaded-prior-to(按上传时间过滤)。
2. 核心命令速查表(重点背诵!)
| 命令 | 语法示例 | 作用说明 | 常用选项 |
|---|---|---|---|
| install | python -m pip install requests | 安装最新版包 | --upgrade, -r requirements.txt, ==1.0.0, >=1.0.0 |
| uninstall | python -m pip uninstall requests | 卸载包 | -y(自动确认) |
| list | python -m pip list | 列出已安装包 | --outdated(显示可升级包) |
| show | python -m pip show requests | 显示包详细信息(版本、依赖、位置) | – |
| freeze | python -m pip freeze > requirements.txt | 导出当前环境依赖(用于备份/部署) | – |
| install -r | python -m pip install -r requirements.txt | 从文件批量安装 | -c constraints.txt |
| download | python -m pip download -d ./wheels requests | 只下载不安装(离线分发用) | --wheel-dir |
| wheel | python -m pip wheel -w ./wheels requests | 构建 wheel 文件(加速离线安装) | – |
| check | python -m pip check | 检查依赖冲突 | – |
| search | python -m pip search "http client" | 在 PyPI 搜索包(旧版可能已废弃) | – |
推荐永远使用的方式(避免多Python版本冲突):
python -m pip install xxx
python3 -m pip install xxx # Linux/Mac 常用
py -m pip install xxx # Windows 推荐
3. 安装包进阶用法(最常用场景)
# 安装指定版本
python -m pip install requests==2.32.3
# 安装最小版本(注意引号)
python -m pip install "requests>=2.30.0,<3.0.0"
# 升级包
python -m pip install --upgrade requests
# 仅为当前用户安装(不影响系统)
python -m pip install --user requests
# 强制重新安装
python -m pip install --force-reinstall requests
# 忽略依赖(不推荐,除非清楚后果)
python -m pip install --no-deps requests
从本地安装:
python -m pip install ./my-package-1.0.0.tar.gz
python -m pip install ./my-wheel-1.0.0-py3-none-any.whl
4. requirements.txt 管理(项目必备!)
# 导出当前环境所有包(精确版本)
python -m pip freeze > requirements.txt
# 安装项目依赖
python -m pip install -r requirements.txt
# 使用约束文件(只限制版本,不强制安装)
python -m pip install -c constraints.txt -r requirements.txt
# 仅安装生产依赖(排除 dev/test)
python -m pip install -r requirements.txt --no-deps
2026新特性:pyproject.toml 中的 [dependency-groups] 支持直接 pip install --group dev。
5. 国内加速镜像(必配!速度提升10倍以上)
临时使用(单次命令):
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
永久配置(推荐):
# 全局设置清华源(最快)
python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 或者阿里云源
python -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
# 查看当前配置
python -m pip config list
2026主流国内镜像(任选其一):
- 清华:https://pypi.tuna.tsinghua.edu.cn/simple
- 阿里:https://mirrors.aliyun.com/pypi/simple/
- 腾讯:https://mirrors.cloud.tencent.com/pypi/simple/
6. 虚拟环境最佳实践(强烈推荐!)
# 创建虚拟环境
python -m venv myenv
# 激活(Windows)
myenv\Scripts\activate
# 激活(Linux/Mac)
source myenv/bin/activate
# 安装包(只影响当前环境)
python -m pip install requests
# 退出环境
deactivate
为什么必须用虚拟环境?
- 避免全局污染
- 不同项目不同版本不冲突
- 部署时可直接复制
requirements.txt
7. 高级与故障排除
# 查看可升级包
python -m pip list --outdated
# 检查依赖冲突
python -m pip check
# 离线安装(先 download)
python -m pip download -d ./wheels -r requirements.txt
python -m pip install --no-index --find-links ./wheels -r requirements.txt
# 清除缓存(解决奇怪问题)
python -m pip cache purge
常见错误解决:
pip is not recognized→ 用python -m pip- 权限错误 → 加
--user或用虚拟环境 - 依赖冲突 → 用
constraints.txt固定版本 - 速度慢 → 配置国内镜像
8. pip 命令速查思维导图(建议收藏)
(实际运行时可生成类似以下结构)
9. 总结 & 推荐工作流
日常开发流程:
- 创建虚拟环境
python -m pip install -r requirements.txt- 开发中
python -m pip install 新包 - 提交前
python -m pip freeze > requirements.txt
一句话口诀:用 -m 跑 pip,用虚拟环境,用清华源,用 requirements.txt 管理依赖。
想看完整项目模板(含 pyproject.toml + 依赖组)、pip 与 poetry/uv 对比,还是企业级离线部署方案?
直接评论回复 “1”“2”“3”,我下一篇文章立刻奉上!
把这篇收藏起来 —— 掌握 pip,你就掌握了 Python 生态的钥匙!🚀
(本文基于 pip 26.0.1 + Python 3.12+ 实测,所有命令均可直接复制运行,数据来自官方文档与2026最新社区教程)