When using just plain pip
to install packages, there is currently no way to make it automatically generate or update a requirements.txt file. It is still a manual process using pip freeze > requirements.txt
.
If the purpose is to make sure the installed packages are tracked or registered properly (i.e. tracked in version control for a repository), then you will have to use other tools that "wrap" around pip
's functionality.
You have two options.
Option 1: Use a package manager
There are a number of Python package managers that combines "install package" with "record installed packages somewhere".
Option 2: git pre-commit hook
This solution isn't going to happen "during installation of the package", but if the purpose is to make sure your tracked "requirements.txt" is synchronized with your virtual environment, then you can add a git pre-commit hook that:
- Generates a separate requirements_check.txt file
- Compares requirements_check.txt to your requirements.txt
- Aborts your commit if there are differences
Example .git/hooks/pre-commit
:
#!/usr/local/bin/bash
pip freeze > requirements_check.txt
cmp --silent requirements_check.txt requirements.txt
if [ $? -gt 0 ];
then
echo "There are packages in the env not in requirements.txt"
echo "Aborting commit"
rm requirements_check.txt
exit 1
fi
rm requirements_check.txt
exit 0
Output:
$ git status
...
nothing to commit, working tree clean
$ pip install pydantic
$ git add .
$ git commit
The output of pip freeze is different from requirements.txt
Aborting commit
$ pip freeze > requirements.txt
$ git add .
$ git commit -m "Update requirements.txt"
[master 313b685] Update requirements.txt
1 file changed, 1 insertion(+)