Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Created August 10, 2011 13:53
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 gdamjan/1136847 to your computer and use it in GitHub Desktop.
Save gdamjan/1136847 to your computer and use it in GitHub Desktop.
A bash function to create a Python PEP-370 virtual environment, similar to virtualenv but leaner
# This is a shell (bash) function. you need to add it to your bashrc or bash_profile file
# Create a PEP-370 Python virtual environment
function create_env () {
ENV=`readlink -f $1` || return 1
mkdir $ENV || return 1
mkdir $ENV/bin || return 1
cat <<-EOF > $ENV/bin/activate
# first check if other env is active
[ x"$PYTHONUSERBASE" != x ] && echo "PYTHONUSERBASE already active from '$PYTHONUSERBASE'. Use 'deactivate' first." && return 1
# setup pep-370
# alternatively ENV=$(dirname $(readlink -f $BASH_SOURCE)/..) would make the activate script stateless
export PYTHONUSERBASE=$ENV
# configure pip
export PIP_INSTALL_OPTION=--user
# setup the bash prompt
_OLD_PS1=\$PS1
PS1="[$1]\$PS1"
# setup the bash path
_OLD_PATH=\$PATH
PATH=\$PYTHONUSERBASE/bin:\$PATH
# .. and at last allow for cleanup
alias deactivate='PATH=\$_OLD_PATH; PS1=\$_OLD_PS1; unset PIP_INSTALL_OPTION; unset PYTHONUSERBASE; unalias deactivate'
EOF
echo "source $ENV/bin/activate in your shell to activate this environment."
}
# end of PEP-370 creator
@gdamjan
Copy link
Author

gdamjan commented Aug 10, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment