Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Created July 12, 2014 21:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miraculixx/5e863e8df7c3461c6fca to your computer and use it in GitHub Desktop.
Save miraculixx/5e863e8df7c3461c6fca to your computer and use it in GitHub Desktop.
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
Copy link
Author

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