参考文献

一篇超详细的 Python 中 pip 常用命令及分类说明,值得收藏

安装包相关命令

安装包:pip install

pip install 是最基本的命令,用于从官方 PyPI(Python Package Index)仓库安装指定的包,

1
2
3
4
pip install 包名

# 示例:安装 requests 库
pip install requests
常用选项:
  • -i:指定镜像源(如 Tsinghua 镜像)
  • --upgrade:升级已安装的包到最新版本
1
2
# 从清华镜像源安装指定版本的 numpy
pip install numpy==1.23.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

升级包:pip install --upgrade

用于将已安装的包升级到最新版本,

1
2
# 示例:升级 pandas
pip install --upgrade pandas

安装指定版本的包

1
2
# 示例:安装 Django 的 4.2 版本
pip install Django==4.2

一次性安装多个包:pip install -r

通过指定 requirements.txt 文件批量安装包

1
2
3
4
5
6
7
# 批量安装requirements.txt中的依赖包,假设requirements.txt文件内容如下
requests==2.28.1
pandas==1.5.3
numpy==1.23.0

# 进入requirements.txt文件所在目录执行下列命令会批量安装上面的包
pip install -r requirements.txt

使用 pip install -r requirements.txt 命令时,需要先进入 requirements.txt 文件所在的目录,然后再执行该命令;如果当前的工作目录(即命令行所在的目录)没有 requirements.txt 文件,pip 会报错如下

1
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

查看信息相关命令

查看已安装的包:pip list

列出当前环境中所有已安装的包及其版本,在Linux中可以使用grep过滤想看的包,在Windows中可以使用findstr过滤想看的包

1
pip list
常用选项:
  • --outdated:列出可以升级的包,示例输出如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Package                   Version   Latest   Type
------------------------- --------- -------- -----
aiohappyeyeballs 2.4.4 2.6.1 wheel
aiohttp 3.10.11 3.11.18 wheel
aiosignal 1.3.1 1.3.2 wheel
altair 4.2.2 5.5.0 wheel
anyio 4.5.2 4.9.0 wheel
async-timeout 4.0.3 5.0.1 wheel
blinker 1.8.2 1.9.0 wheel
exceptiongroup 1.2.2 1.3.0 wheel
frozenlist 1.5.0 1.6.0 wheel
greenlet 3.1.1 3.2.2 wheel
importlib_resources 6.4.5 6.5.2 wheel
zipp 3.20.2 3.21.0 wheel
  • --format:指定输出格式(如 legacycolumns

查看包详情:pip show

显示某个包的详细信息,包括版本号、安装位置、依赖项等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pip show numpy

# 示例输出
(Juke_AI_Project) PS D:\Python Project\Juke_AI_Project\day3-demo\chatbot-app> pip show pandas
Name: pandas
Version: 2.0.3
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author:
Author-email: The Pandas Development Team <pandas-dev@python.org>
License: BSD 3-Clause License
Location: e:\anaconda3\envs\juke_ai_project\lib\site-packages
Requires: numpy, python-dateutil, pytz, tzdata
Required-by: altair, streamlit

卸载包相关命令

卸载包:pip uninstall

用于从当前环境中移除指定包

1
2
3
4
pip uninstall 包名

# 示例
pip uninstall requests

搜索与缓存相关命令

清理缓存:pip cache purge

pip 会将下载的包存储到本地缓存中,使用该命令可以清除缓存

1
pip cache purge

检查与调试相关命令

检查依赖关系:pip check

用于检查已安装的包是否存在依赖关系冲突

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pip check

# 示例
(Juke_AI_Project) PS D:\Python Project\Juke_AI_Project\day3-demo\chatbot-app> pip check
MarkupSafe 2.1.5 is not supported on this platform
PyYAML 6.0.2 is not supported on this platform
aiohttp 3.10.11 is not supported on this platform
charset-normalizer 3.4.2 is not supported on this platform
frozenlist 1.5.0 is not supported on this platform
greenlet 3.1.1 is not supported on this platform
jiter 0.9.0 is not supported on this platform
multidict 6.1.0 is not supported on this platform
orjson 3.10.15 is not supported on this platform
pandas 2.0.3 is not supported on this platform

导出与备份环境

导出当前环境的包:pip freeze

将当前环境(或者conda的虚拟环境)所有已安装包的版本信息输出,可保存到文件以备后用,要注意当前所在目录,会导出到当前执行pip命令所在的目录上,一般用requirements.txt作为依赖文件名,用别的也可以,但不规范

1
pip freeze > requirements.txt

导出结果示例

通过导出的文件重新安装:pip install -r

结合 pip freeze,可以在新环境中快速恢复所有依赖包,注意requirements.txt文件所在的位置问题

1
2
# 示例:恢复环境
pip install -r requirements.txt

镜像源配置

国内用户使用 pip 时,建议设置国内镜像源(如 Tsinghua),以加快下载速度,

1
2
#  示例:临时使用镜像源
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

永久配置镜像源可参考网上各种教程

pip命令常用参数列表

1
2
# 示例
pip install -qU langchain-openai
  • -q:安静模式,减少输出日志。

  • -U:升级,若已安装则更新到最新版。

参数 说明
-q 安静模式,减少输出日志
-U 升级,若已安装则更新到最新版
-r 从指定文件安装依赖,如 requirements.txt
-i 指定包索引源,如 -i https://pypi.org/simple
--upgrade 强制升级到最新版(同 -U
--no-cache-dir 禁用缓存,直接从源下载
--force-reinstall 强制重新安装,即使已存在
-v 详细模式,显示更多安装日志
--no-deps 不安装依赖包
--target 指定安装目录,如 --target=/path
--pre 包含预发布和开发版本
--user 安装到用户目录,非系统全局