Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
add and remove pip install/uninstall from requirements.txt automatically
# add pip install/uninstall to requirements.txt automatically
pipr() {
if [ "$1" == "install" ]; then
pip $1 $2
pip freeze | grep -i $2 >> requirements.txt
echo ok, added $2 as:
tail -n1 requirements.txt
if [ "$1" == "uninstall" ]; then
echo y | pip $1 $2 >> .pipremoved
grep -i "successfully uninstalled" .pipremoved | grep -o -i $2 >> requirements.removed
cp requirements.txt requirements.bak
tail -n1 requirements.removed | xargs -I{} grep -v {} requirements.bak > requirements.txt
echo ok, removed:
tail -n1 requirements.removed
rm .pipremoved >/dev/nul

This comment has been minimized.

Copy link
Owner Author

miraculixx commented Jul 12, 2014


# install some-package
$ pipr install some-package
$ cat requirements.txt

# remove some-package
$ pipr uninstall somepackage
$ cat requirements.txt

so you don't have to manually manage requirements.txt. You could use pip freeze > requirements.txt but this generates a lot of clutter: pip freeze does not take into account dependencies, and so once you install more than a couple of packages, your requirements.txt becomes cluttered. pipr only adds what you actually installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.