Python生成requirements.txt方法

Python生成requirements.txt方法

在查看别人的Python项目时,经常会看到一个requirements.txt文件,里面记录了当前程序的所有依赖包及版本号,其作用是用来在另一个环境上重新构建项目所需要的运行环境依赖。

方法一:

requirements.txt可以通过pip命令自动生成和安装,这种情况更适用于此项目是单独的虚拟python环境
生成requirements.txt文件

pip freeze > requirements.txt

安装requirements.txt依赖

pip install -r requirements.txt

方法二:

使用pipreqs github地址
CTOLib码库对这个模块的描述是:从真实的代码挖掘出项目真正导入的模块, 并组织为 pip requirements.txt
安装

pip install pipreqs

用法

pipreqs /home/project/location

以下摘自github: 
Why not pip freeze?
pip freeze only saves the packages that are installed with pip install in your environment.
pip freeze saves all packages in the environment including those that you don't use in your current project. (if you don't have virtualenv)
and sometimes you just need to create requirements.txt for a new project without installing modules.

原文地址:https://www.cnblogs.com/flyfish919/p/7711394.html