pip常用指令小结
pip 包管理工具使用说明
pip
(Python Installer Package)是 Python 官方推荐的包管理工具,用于安装、升级、卸载 Python 第三方库。
操作命令
操作命令 | 用途说明 |
---|---|
pip --version | 查看 pip 版本,验证是否已安装 |
pip install 包名 | 安装指定 Python 包 |
pip install 包名==版本号 | 安装指定版本的包 |
pip install -r requirements.txt | 批量安装依赖包(常用于项目环境搭建) |
pip uninstall 包名 | 卸载已安装的包 |
pip list | 列出当前环境中已安装的包及版本号 |
pip show 包名 | 显示某个包的详细信息(安装路径、版本、依赖等) |
pip freeze > requirements.txt | 导出当前环境依赖(用于迁移环境) |
pip install --upgrade 包名 | 升级已安装的包 |
pip help | 查看 pip 帮助文档,获取更多命令参数说明 |
源
通过国内镜像下载
可在 pip install 包名
后面增加国内镜像源参数,例如:
# 阿里云镜像
pip install 包名 -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
# 清华大学镜像
pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
# 中国科学技术大学镜像
pip install 包名 -i https://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn
永久生效
- 对于 macOS、Linux,通过修改
~/.config/pip/pip.conf
文件。 - 对于 Windows,通过修改
%appdata%\pip\pip.ini
文件。
在文件中添加如下内容(以阿里云镜像为例):
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
相当于自动加上后缀 "-i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com"
。
Tip: 安装包含torch环境时常见问题
解决方法:指定uv寻找的网址
uv pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu117