Skip to content

Instantly share code, notes, and snippets.

@sinewalker
Created May 21, 2019 02:16
Show Gist options
  • Save sinewalker/6733343f98c6895e55f3280f34051d33 to your computer and use it in GitHub Desktop.
Save sinewalker/6733343f98c6895e55f3280f34051d33 to your computer and use it in GitHub Desktop.
Freeze your python pip requirements to a known place, to guard against Homebrew mess-ups
freezenv() {
FUNCDESC='Freeze the active Python Environment pip requirements.
This stores the requirements.txt in the active $VIRTUAL_ENV or $CONDA_PREFIX
directory, overwriting any existing requirements file.'
if [[ -z ${VIRTUAL_ENV-$CONDA_PREFIX} ]] ; then
error "$FUNCNAME: no active python or conda venv"
return 1
fi
local VENV_REQS=${VIRTUAL_ENV-$CONDA_PREFIX}/requirements.txt
echo "Storing PIP package list into ${VENV_REQS}"
pip freeze > ${VENV_REQS}
}
@sinewalker
Copy link
Author

sinewalker commented May 21, 2019

  • Replace error with echo if you don't have an error function that writes to STDERR

Why? Because whenever you update python in Homebrew on a Mac, it makes a mess of your existing python virtual environments that link to libraries and executables that no longer exist. So you have to re-create the virtual environment.

Only because it's broken, you can't activate the venv to do a pip freeze after the fact.

So, whenever you add a package in a venv with pip install somepackage you should then freezenv to keep a record of the installed packages.

Probably I should make another function to install-and-freeze in one go...

@sinewalker
Copy link
Author

sinewalker commented May 21, 2019

not really necessary for Anaconda, since Homebrew won't mess with that. But I include the functionality for conda too, in case it's needed for other reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment