TL;DR:

Open up your .zshrc or .bashrc file and paste the following into the bottom:

# pip install and save to (and sort + dedupe) requirements.txt
	function pip-save {
	    # loop through all listed requirements
	    for var in "$@"
	    do
	        # attempt to install it
	        pip install $var

	        # add it to the requirements.txt file
	        pip freeze | grep -i "^$var=" >> requirements.txt
	    done

	    # sort requirements.txt and remove duplicates
	    sort -u requirements.txt -o requirements.txt
	}

Now you can do: pip-save django psycopg2-binary and get an updated requirements.txt with a locked in version, and move on with what you actually set out to do.

Motivation

After almost a decade as Node.js developer, I’m back in Python land.

One immediate pet peeve when spinning up new services was how much more typing and copy/pasting I was doing to install dependencies.

Installing a JS dependency (e.g. express and mongoose):

  1. npm install --save express mongoose
  2. End of list. Move on with life.

#programming #flask #python #python3 #django

One-step Install+Save for Python Dependencies
1.40 GEEK