Skip to content

Instantly share code, notes, and snippets.

@davur
Created September 20, 2022 06:18
Show Gist options
  • Save davur/0252e7822de688b7593281c6a2ec36b8 to your computer and use it in GitHub Desktop.
Save davur/0252e7822de688b7593281c6a2ec36b8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# This script must be sourced from within a shell
# and not executed. For instance with:
#
# . path/to/script/workon.sh
function workon() {
curdir=${PWD##*/} # to assign to a variable
curdir=${curdir:-/} # to correct for the case where PWD=/
parent=$(dirname $PWD)
parent=${parent##*/}
parent=${parent:-/}
combo="${parent}_${curdir}"
venv_path="${HOME}/virtualenvs/${combo}"
if [ ! -z ${VIRTUAL_ENV+x} ] ; then
deactivate
fi
if [ ! -d "${venv_path}" ]; then
python3.9 -m venv "${venv_path}"
fi
source "${venv_path}/bin/activate"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment