How to Generate requirements.txt Using pip freeze and pipreqs
This guide explains two methods for creating a Python project's requirements.txt file—using pip freeze within a virtualenv and using the pipreqs tool to automatically scan source code—highlighting their commands, advantages, and the need for manual verification of the generated dependencies.
When developing a Python project, a requirements.txt file is needed to list third‑party packages; while pip freeze works in a virtualenv, mixed environments without a maintained list can be problematic.
The article first shows the standard method of running pip freeze > requirements.txt from the project root, which reliably captures the environment when a virtualenv is used.
It then introduces the pipreqs tool, which scans the source directory, automatically detects imported libraries, and generates a requirements.txt file, though the output may need manual verification and adjustment.
Installation and usage are simple:
# pip install pipreqs
# pipreqs ./An example of the generated requirements.txt is shown, listing packages such as AdvancedHTMLParser, aiotkinter, alembic, and many others. The article notes that occasional inaccuracies may appear, so the file should be reviewed and corrected as needed.
Overall, the guide provides two practical approaches for creating a Python dependency list, recommending pip freeze for virtualenvs and pipreqs as an automated alternative.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.