Skip to content

Instantly share code, notes, and snippets.

@drslump
Created March 30, 2018 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drslump/e71f4108a30f9b14ff4d73905458049e to your computer and use it in GitHub Desktop.
Save drslump/e71f4108a30f9b14ff4d73905458049e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Summary: Creates a temporary virtualenv based on a version
#
# Usage: pyenv COMMAND [version] [tmp-name]
#
# When no version is provided it'll use the currently configured
# one.
#
set -e
[ -n "$PYENV_DEBUG" ] && set -x
VERSION="$1"
NAME="$2"
[ -z "$VERSION" ] && VERSION=$( pyenv version-name )
[ -z "$NAME" ] && NAME=$(mktemp -u "tmp-$VERSION-XXXX")
pyenv versions --bare | grep -qx "$VERSION" || {
>&2 echo "WARNING: triggering $VERSION install"
pyenv install "$VERSION"
}
>&2 echo "Creating temporary virtualenv '$NAME' (from $VERSION)..."
pyenv virtualenv "$VERSION" "$NAME" &> /dev/null || {
>&2 echo 'ERROR: Unable to create temporary virtualenv'
exit 1
}
trap 'pyenv uninstall -f "$NAME"' EXIT
>&2 echo "Entering shell with virtualenv '$NAME' (removed on exit)"
PYENV_VERSION="$NAME" "$SHELL"
>&2 echo "Destroying temporary virtualenv '$NAME'..."
# the EXIT trap will uninstall it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment