0

Is there a way to create a requirements.txt file that only contains the modules that my script actually needs?

I usually just do a pip freeze and then remove unused modules.

1 Answers1

1

You can use pipreqs to analyze your project files, and automatically generate a requirements.txt for you.

First step is to install pipreqs. You can do so, by executing the following command in your console:

pip install pipreqs

Then, the only thing you need to do is to execute the command pipreqs, specifying the path where your files are located at. For example, to generate a requirements.txt, based on the modules on the current working directory, you could execute:

pipreqs .

For some other directory:

pipreqs "PATH/TO/YOUR/PROJECT/PYTHON/FILES"

Real World Example

Here's the tree-view of the folder we're going to use as example:

.
├── data
│   └── EXCEL_FILES
│       ├── DIVISION_MAP.xlsx
│       ├── INPUT_CONTAINER.xlsx
│       ├── KITS.xlsx
│       ├── LOADING.xlsx
│       ├── MOQ_CBM_SHIPPING_TYPE.xlsx
│       ├── OUTBOUND_OTM.XLSX
│       ├── PLANT_SOURCE_MAP.xlsx
│       ├── PORT_STATE.xlsx
│       └── SALABLE_STORAGE_LOCATION.xlsx
├── loading
│   ├── __init__.py
│   ├── configs
│   │   ├── Initialization.py
│   │   ├── __init__.py
│   │   ├── config.ini
│   │   └── logconfig.py
│   ├── constants.py
│   ├── read_files.py
│   ├── reports.py
│   └── utils
│       ├── __init__.py
│       ├── date_utils.py
│       ├── file_utils.py
│       └── utils.py
├── logs
│   └── po_opt.log
├── main.py
└── outputs
    ├── 2022-10-03
    ├── 2022-10-04
    ├── 2022-10-05
    ├── 2022-10-06
    ├── 2022-11-23
    ├── 2022-11-24
    └── 2022-11-28

Executing the command pipreqs . from the root path I get the following requirements.txt:

❯ pipreqs .
INFO: Successfully saved requirements file in ./requirements.txt

Output:

holidays==0.17.2
numpy==1.20.1
pandas==1.2.4
python_dateutil==2.8.2
six==1.16.0
Ingwersen_erik
  • 1,701
  • 1
  • 2
  • 9