跳转至

pipreqs生成requirements.txt文件

GitHub仓库:https://github.com/bndr/pipreqs

一. 安装pipreqs

pip install pipreqs

二. 生成requirements.txt文件

在当前目录下,使用以下命令生成requirements.txt文件:

pipreqs . --encoding=utf8 --force

三.示例

假设你有一个项目目录,项目结构如下:

my_project/
│
├── main.py
└── utils.py

main.pyutils.py中,你使用了requestspandas这两个库。此时,你可以打开终端,进入my_project目录,并运行上述命令(pipreqs . --encoding=utf8 --force)。生成的requirements.txt文件内容可能如下:

requests==2.26.0
pandas==1.3.3

注意

  • --encoding=utf8表示使用utf8编码,避免出现UnicodeDecodeError: 'gbk'这样的错误。
  • --force表示强制执行,如果当前目录下已存在requirements.txt文件,将覆盖原文件。