python依赖管理方案
pip作为python默认的包管理工具, 提供了在线安装python依赖包的工具. 但是内置的pip freeze默认打包整个机器上的python依赖环境, 不太友好. 下面简单介绍4个常用的依赖管理工具.
pip
pip freeze > requirements.txt 打包整个机器上的python环境并重定向到requirements.txt文档之中
pip install -r requirements.txt 根据以来描述文件安装对应依赖
pipreqs
pipreqs
对给定文件夹中的python依赖进行解析, 并且生成依赖描述文件requirements.txt
后续可以直接使用pip install -r requirements.txt来安装依赖
pip-tools
使用pip-tools可以精准的控制项目依赖
pip-compile 可以在setup.py文件或者requirements.in文件中递归的解析出完整的依赖路径. 并将其输出到requirements.txt之中.
(这里的setup.py是python应用的分发工具. 暂时还用不上. 直接写出requirements.in就行了.)
# pip-compile
## 用例
pip-compile 默认根据setup.py生成依赖描述文件
pip-compile 默认根据requirements.in生成依赖描述文件
## 这里可以通过两个不同名的依赖描述文件来管理两套环境
## 另外requirements.in中可以通过-c选项来嵌套的引入requirements.txt约束
## 常用参数
--upgrade 根据依赖描述文件更新所有包
-P --upgrade-package <pkg_name> 对指定包进行更新(可以加上版本限制)
--output-file 指定输出文件
pip-sync 需要安装在与项目相同的虚拟环境之中. (这里可以和conda配合使用, 在conda的虚拟环境中安装pip-sync)
pip-sync可以让当前的虚拟环境与pip-compile生成的requirements.txt文件相一致.
# pip-sync
## 用例
pip-sync <requirements-file> 可以同时同步多个环境. 当然, 这里的默认值为requirements.txt
pigar
pigar -s选项可以从import name中查找出真实的依赖库
pipdeptree
用于展示解析出项目依赖的树形结构
pip具体使用方法
pip主要用于python的包管理, 具体用法可以直接通过pip -h来进行查询. 这里就不加以赘述了.
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.