Skip to content

Instantly share code, notes, and snippets.

@jakob1379
Created December 19, 2019 09:32
Show Gist options
  • Select an option

  • Save jakob1379/589a2e74e1b5b432199c8e632cccbedc to your computer and use it in GitHub Desktop.

Select an option

Save jakob1379/589a2e74e1b5b432199c8e632cccbedc to your computer and use it in GitHub Desktop.
An extension for pyenv virtualenv to instantiate a virtual environment and associate the local folder with the same virtual environment
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
displayUsage() {
echo '
An extension for pyenv virtualenv to instantiate a virtual environment and
association of the local folder with the same virtual environment
usage: pyenvdir [pyver] <name>
operations:
pyenvdir {-h help} shows shis dialogue
pyenvdir {-p version} python version to use e.g. 3.7.5
pyenvdir {-n name} name of virtual environment
pyenvdir {-l list} list available python versions
'
}
# Set defaults
PYVER=''
# Go through optional arguments
while getopts ":hlp:" opt; do
case ${opt} in
l)
pyenv versions
;;
p)
PYVER=${OPTARG}
;;
h)
displayUsage
exit 1
;;
\?)
echo "Invalid option: $OPTARG" 1>&2
displayUsage
exit 2
;;
:)
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 2
;;
esac
done
shift $(( OPTIND - 1 ))
OPTIND=1
# Check for mandatory arguments
if [ $# -lt 1 ]
then
echo '
Not enough args provided. At least provide a name'
displayUsage
fi
ENVNAME=$1
# init and associate pyenv virtualenv
pyenv virtualenv "$PYVER" "$ENVNAME"
pyenv local "$ENVNAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment