pip 使用
下面给你一份完整、零基础、实战可用的 Python pip 国内镜像 + 常用命令教程,Windows / macOS / Linux 通用,直接照着做就行。
一、pip 是什么?
- 是 Python 的包管理工具
- 用来安装、卸载、更新各种库(torch、vllm、flask 等)
- 命令格式:
pip install 包名
二、为什么要换国内镜像?
官方源在国外,下载极慢、经常超时、报错。
国内镜像速度快 10~100 倍。
常用国内镜像源
| 镜像 | 地址 |
|---|---|
| 阿里云(推荐) | https://mirrors.aliyun.com/pypi/simple/ |
| 清华大学 | https://pypi.tuna.tsinghua.edu.cn/simple |
| 中国科技大学 | https://mirrors.ustc.edu.cn/pypi/simple/ |
| 豆瓣 | https://pypi.doubanio.com/simple/ |
三、设置镜像的 3 种方式(从简单到高级)
方式1:临时使用(一次有效)
安装包时加 -i 指定镜像:
pip install vllm -i https://mirrors.aliyun.com/pypi/simple/
方式2:永久设置(推荐)
2.1 设置阿里云(最稳)
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com
2.2 设置清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set install.trusted-host pypi.tuna.tsinghua.edu.cn
2.3 查看当前配置
pip config list
2.4 恢复官方默认源
pip config unset global.index-url
方式3:手动改配置文件(进阶)
Windows
文件路径:
C:\Users\你的用户名\pip\pip.ini
没有就新建 pip 文件夹,再新建 pip.ini
内容:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
macOS / Linux
文件路径:
~/.config/pip/pip.conf
内容同上。
四、pip 最常用命令(必背)
1. 安装包
pip install 包名
示例:
pip install torch
pip install vllm
pip install flask
2. 指定版本安装
pip install 包名==版本号
pip install torch==2.2.0
3. 卸载包
pip uninstall 包名
4. 查看已安装包
pip list
5. 查看某个包信息
pip show 包名
6. 升级包
pip install --upgrade 包名
7. 升级 pip 自身
pip install --upgrade pip
8. 导出当前环境依赖
pip freeze > requirements.txt
9. 批量安装
pip install -r requirements.txt
10. 清理缓存
pip cache purge
五、常见问题解决
1. 提示 SSL 错误
加 trusted-host:
pip install vllm -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
2. 提示权限不足
Windows:用管理员终端
macOS/Linux:
pip install --user 包名
3. 多个 Python 版本混淆
- Python3.10 →
pip3.10 - Python3.11 →
pip3.11 - 或用:
python -m pip install 包名
4. 下载仍然很慢
- 换镜像
- 清理缓存
pip cache purge - 检查网络
六、最推荐的最终配置
直接复制这两行,永久提速、不报错:
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com
本文来自博客园,作者:蓝迷梦,转载请注明原文链接:https://www.cnblogs.com/hewei-blogs/articles/19821840

浙公网安备 33010602011771号