Skip to content

Instantly share code, notes, and snippets.

@arthurpaulino
Created June 26, 2022 01:43
Show Gist options
  • Save arthurpaulino/6be820b0869e1adb78e0fae7ccbedd0c to your computer and use it in GitHub Desktop.
Save arthurpaulino/6be820b0869e1adb78e0fae7ccbedd0c to your computer and use it in GitHub Desktop.
A bash script to wrap Python's `venv`
# You can append the following functions to your `.profile` file
function venv_help() {
echo "venv list ....... list environments"
echo "venv del myenv .. delete 'myenv'"
echo "venv off ........ deactivate the current environment"
echo "venv myenv ...... create 'myenv' if it doesn't exist then activate it;"
echo " just activate it otherwise"
}
function venv() {
case $1 in
list)
if ! [ -z $2 ]
then
venv_help
else
ls $HOME/.venv
fi
;;
off)
if ! [ -z $2 ]
then
venv_help
else
if [[ $(which python) = $HOME/.venv/* ]]
then
deactivate
fi
fi
;;
del)
if [ -z $2 ]
then
venv_help
else
local env=$HOME/.venv/$2
if [[ $(which python) = $env/bin/python ]]
then
deactivate
fi
if [ -f $env/bin/python ]
then
rm -rf $HOME/.venv/$2
else
echo "environment not found: $2"
fi
fi
;;
*)
if [ -z $1 ]
then
venv_help
else
local env=$HOME/.venv/$1
if [ -d $env ]
then
if ! [[ $(which python) = $env/bin/python ]]
then
source $env/bin/activate
fi
else
python3 -m venv $env
source $env/bin/activate
fi
fi
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment