Skip to content

Instantly share code, notes, and snippets.

@napisani
Created October 10, 2021 14:55
Show Gist options
  • Save napisani/cad0284baa15e061cdd6e62aa1b8b083 to your computer and use it in GitHub Desktop.
Save napisani/cad0284baa15e061cdd6e62aa1b8b083 to your computer and use it in GitHub Desktop.
direnvrc
# === Use a specific python version (with pyenv) ===
# Usage in .envrc:
# use python <version>
use_python() {
if has pyenv; then
local pyversion=$1
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
pyenv local ${pyversion} || log_error "Could not find pyenv version '${pyversion}'. Consider running 'pyenv install ${pyversion}'"
fi
}
# === Support for Python >=3.3 venv ===
# See https://github.com/direnv/direnv/wiki/Python#venv-stdlib-module
# Python 3.3 and later provide built-in support for virtual environments via the venv module in the standard library.
# Usage in .envrc:
# export VIRTUAL_ENV_HOME=.venv # Default: ~/venv
# layout python-venv <name> # creates ${VIRTUAL_ENV_HOME}/<name> venv
realpath() {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
layout_python-venv() {
local pvenv=$1
VIRTUAL_ENV_HOME=${VIRTUAL_ENV_HOME:-~/venv}
VIRTUAL_ENV=${VIRTUAL_ENV:-${VIRTUAL_ENV_HOME}/${pvenv}}
unset PYTHONHOME
export VIRTUAL_ENV
if [[ ! -d $VIRTUAL_ENV ]]; then
log_status "no venv found; creating $VIRTUAL_ENV"
python3 -m venv "$VIRTUAL_ENV"
fi
PATH_add "${VIRTUAL_ENV}/bin"
}
# === Create a new virtualenv ===
# Usage in .envrc:
# layout virtualenv <version> <name>
layout_virtualenv() {
local pyversion=$1
local pvenv=$2
if has pyenv; then
pyenv local ${pyversion}
if [ -n "$(which pyenv-virtualenv)" ]; then
eval "$(pyenv virtualenv-init -)"
pyenv virtualenv --force --quiet ${pyversion} ${pvenv}
else
log_error "pyenv-virtualenv is not installed."
fi
elif has python3; then
# Use venv by default
layout_python-venv ${pvenv}
else
log_error "pyenv or python3 venv not found."
fi
}
# === Activate a virtualenv ===
# Note that pyenv-virtualenv uses 'python -m venv' if it is
# available (CPython 3.3 and newer) and 'virtualenv' otherwise
# Usage in .envrc:
# layout activate <name>
layout_activate() {
if has pyenv; then
local pyenvprefix=$(pyenv prefix)
local pyversion=$(pyenv version-name)
local pvenv="$1"
# Below initialization is necessary to recall ;(
pyenv activate ${pvenv}
else
source ${VIRTUAL_ENV}/bin/activate
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment