Python项目中的requirements文件
requirements.txt的格式
在Python项目中,通常会包含一个 requirements.txt 文件,其中列出了项目运行所需的Python库及其版本信息。该文件用于部署Python项目的运行环境,并管理项目的依赖包,以确保代码能够在新部署的Python环境中正常运行。
requirements.txt 文件的每一行都包含一个库的名称和版本号信息(可选),支持的格式如下:
- librosa:这行没有指定版本号,只指定了库的名称。
- numpy==1.19.2:这行指定了库的名称为numpy,并且要求精确的版本号为1.19.2。
- pandas>=1.1.3:这行指定了库的名称为pandas,并且要求最低版本号为1.1.3,包括该版本和更高版本。
- scikit-learn~=0.23.2:这行指定了库的名称为scikit-learn,并且要求兼容的最低版本号为0.23.2,包括该版本和具有相同主版本号的更高版本。
使用requirements.txt文件安装依赖库
当你有了requirements.txt文件,你可以使用pip(Python的包管理器)来安装所有的依赖。你只需要运行以下命令:
pip install -r requirements.txt
这将会自动安装文件中列出的所有包及其指定版本。
创建requirements.txt文件
创建requirements.txt文件的一种常见方式是使用pip freeze命令,这个命令可以列出当前Python环境中安装的所有包及其版本。你可以把这个列表重定向到一个文件中,创建你的requirements.txt文件,如下所示:
pip freeze > requirements.txt
这样,requirements.txt文件就会包含你当前环境中所有的Python包及其精确版本。
Python项目依赖的包?最小+精准
使用pip freeze命令只会列出当前Python环境中安装的包,与Python项目本身无关。
以下是关于pip freeze的几点说明:
- pip freeze只会分析当前Python环境中安装的库。
- pip freeze根本不会解析Python项目中的任何Python源码。
- 开发人员的Python环境通常只会运行他们负责的Python项目代码,因此从这个环境得到的requirements.txt可能并不包含所有的依赖包。
- 运行Full Test的Python环境可以调用项目中的所有代码,但从这个环境得到的requirements.txt不一定是最小的依赖包列表,通常会包含许多不必要的依赖包。
为了获得Python项目的精确依赖包列表,可以使用pipreqs工具。pipreqs能够扫描项目,找出项目实际依赖的所有包,并生成requirements.txt文件。
安装pipreqs
 
- 使用以下命令安装:pip install pipreqs
- 如果使用conda环境,可以使用以下命令安装:conda install pipreqs
pipreqs 的命令行参数
 
pipreqs - Generate pip requirements.txt file based on imports
Usage:pipreqs [options] [<path>]
Arguments:<path>                The path to the directory containing the applicationfiles for which a requirements file should begenerated (defaults to the current workingdirectory).
Options:--use-local           Use ONLY local package info instead of querying PyPI.--pypi-server <url>   Use custom PyPi server.--proxy <url>         Use Proxy, parameter will be passed to requestslibrary. You can also just set the environmentsparameter in your terminal:$ export HTTP_PROXY="http://10.10.1.10:3128"$ export HTTPS_PROXY="https://10.10.1.10:1080"--debug               Print debug information--ignore <dirs>...    Ignore extra directories, each separated by a comma--no-follow-links     Do not follow symbolic links in the project--encoding <charset>  Use encoding parameter for file open--savepath <file>     Save the list of requirements in the given file--print               Output the list of requirements in the standardoutput--force               Overwrite existing requirements.txt--diff <file>         Compare modules in requirements.txt to projectimports--clean <file>        Clean up requirements.txt by removing modulesthat are not imported in project--mode <scheme>       Enables dynamic versioning with <compat>,<gt> or <non-pin> schemes.<compat> | e.g. Flask~=1.1.2<gt>     | e.g. Flask>=1.1.2<no-pin> | e.g. Flask
以下是 pipreqs 命令行参数的详细说明:
- <path>: 包含应用程序文件的目录的路径,用于生成- requirements.txt文件(默认为当前工作目录)。
- -use-local: 仅使用已经安装的本地包信息,而不查询 PyPI。
- -pypi-server <url>: 使用自定义的 PyPi 服务器。
- -proxy <url>: 使用代理,该参数将传递给- requests库。你也可以在终端中设置环境变量:- $ export HTTP_PROXY="<http://10.10.1.10:3128>"
- $ export HTTPS_PROXY="<https://10.10.1.10:1080>"
 
- -debug: 打印调试信息。
- -ignore <dirs>...: 忽略额外的目录,每个目录以逗号分隔。
- -no-follow-links: 不要跟随项目中的符号链接。
- -encoding <charset>: 使用指定的字符编码打开文件。
- -savepath <file>: 将依赖列表保存到指定的文件中。
- -print: 将依赖列表输出到标准输出。
- -force: 覆盖现有的- requirements.txt文件。
- -diff <file>: 将- requirements.txt中的模块与项目导入的模块进行比较。
- -clean <file>: 清理- requirements.txt,删除项目中未导入的模块。
- -mode <scheme>: 启用动态版本管理,可使用- <compat>、- <gt>或- <non-pin>方案。- <compat>: 例如- Flask~=1.1.2
- <gt>: 例如- Flask>=1.1.2
- <no-pin>: 例如- Flask
 
pipreqs 的例子
 
以下是使用 pipreqs 生成 requirements.txt 文件的几个示例:
-  生成当前目录下Python项目的 requirements.txt文件,并覆盖现有的requirements.txt文件:pipreqs . --force
-  生成指定目录下Python项目的 requirements.txt文件,并将结果保存到指定文件中:pipreqs /path/to/project --savepath custom_requirements.txt
-  生成当前目录下Python项目的 requirements.txt文件,并将结果输出到标准输出:pipreqs . --print
pipreqs和pip freeze 生成requirements.txt 的比较
 
-  pipreqs工具与pip freeze命令都可以用来生成requirements.txt文件,但它们之间有一些区别。- pip freeze命令只会列出当前Python环境中安装的包,并不会解析项目中的任何Python源码。这意味着从- pip freeze生成的- requirements.txt文件可能不包含项目的所有依赖包,并且可能包含一些不必要的依赖包。
- pipreqs工具能够扫描项目中的Python源码,找出项目实际依赖的所有包,并生成一个精确的- requirements.txt文件。这样可以确保- requirements.txt文件中只包含项目所需的最小依赖包列表。
- pipreqs生成的依赖包列表可能会出现重复的项(这可能是- pipreqs的一个bug)。扫描过程相对复杂,因此很难确定是否会出现bug。而- pip freeze则是一个简单的安装文件扫描,不容易出现问题。
 
-  requirements.txt文件中的包名格式也有区别:- pipreqs:扫描Python源文件中的包名,包名中不同单词之间的分隔符是下划线“- _”,例如“- websocket_client”。- pip freeze:输出的包名是Python安装包在PyPI(Python包索引)中的名称列表。许多包在PyPI中的名称都是以减号“``”分隔的,例如“- websocket-client”。
- pipreqs:扫描Python源文件中的包名,包名的大小写与源代码中的包名大小写格式一致,例如“- Pillow”。- pip freeze中的包名来自PyPI的信息,例如“- pillow”。
 
👌🏻 无需担心
pip会忽略包名的大小写,并且可以自动处理下划线和减号的转换。