跳转至

pip 管理项目依赖

概要: 使用pip工具管理Python项目的包依赖

创建时间: 2022.12.17 14:46:52

更新时间: 2023.07.28 22:54:56

提示

操作前,务必执行pip -V确认当前使用的Python环境

更换源

更换为国内源

从下面各个源中找到速度最快的设置一个即可。
腾讯源

Bash
pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple
阿里源
Bash
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
清华大学源
Bash
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
中科大源
Bash
pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple
上海交大源
Bash
pip config set global.index-url https://mirror.sjtu.edu.cn/pypi/web/simple

恢复使用默认源

Bash
pip config unset global.index-url

安装包

列出所有已安装的包

Bash
pip list

安装包(不指定版本)

Bash
pip install ${package_name}

安装包(指定版本)

Bash
# 其中x y z为版本号
pip install ${package_name}==x.y.z

移除包

Bash
pip uninstall ${package_name}

升级包

升级pip自身

Bash
pip install -U pip

列出可升级的包

Bash
pip list --outdate

升级特定包

Bash
pip install --upgrade ${package_name}

requirements.txt

requirements.txt是Python项目最为常见且最为纯粹的依赖描述文件,记录了当前Python项目需要哪些包的哪些版本

生成依赖

Bash
pip freeze > requirements.txt

安装依赖

Bash
pip install -r requirements

升级依赖

Bash
pip install --upgrade -r requirements

移除依赖

Bash
pip uninstall -r requirements

FAQ

Fedora安装py包提示 /bin/sh: 行 1: krb5-config: 未找到命令

解决方法: 安装缺失的krb5-devel包即可

Bash
sudo dnf install krb5-devel

参考