Skip to content

Instantly share code, notes, and snippets.

Embed
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
fi
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
fi
}
@miraculixx

This comment has been minimized.

Copy link
Owner Author

miraculixx commented Jul 12, 2014

usage:

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

# 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.