The Python requirement file is a way of documenting the Python modules used in a project. It is a simple text file that lists the modules and packages required by the project. The file contents are generally similar to this (based on the Ansible project’s requirements.txt file):
jinja2 >= 3.0.0
PyYAML >= 5.1 # PyYAML 5.1 is required for Python 3.8+ support
cryptography
packaging
resolvelib >= 0.5.3, < 1.1.0 # dependency resolver used by ansible-galaxy
To create a requirements.txt file in a Python project, we can use the following command in the project root directory (with the virtual environment activated):
py -m pip freeze > requirements.txt
To install the dependencies for a project using the requirements file, we would use:
py -m pip install -r requirements.txt
Similarly, if we want to uninstall the dependencies for an existing project, we can use the command:
py -m pip uninstall -r requirements.txt -y
Note, if we want to uninstall all existing dependencies when we don’t have a requirements file, we can use the command:
pip uninstall -y -r <(pip freeze)
The above was based on the this StackOverflow answer.