Skip to content

Instantly share code, notes, and snippets.

@gcv
Created October 27, 2015 04:56
Show Gist options
  • Save gcv/2f60ee32202fc1d32462 to your computer and use it in GitHub Desktop.
Save gcv/2f60ee32202fc1d32462 to your computer and use it in GitHub Desktop.
Python isolated virtualenv scripts
function pve() {
local pd=~/.python
local env_name=$1
if [[ -z ${env_name} ]]; then
if [[ -z ${VIRTUAL_ENV} ]]; then
echo "usage: pve{2,3} <envname>"
return 1
else
deactivate
return 0
fi
fi
if [[ ! -e ${pd} ]]; then
echo "${pd} not found"
return 1
fi
local system_python=$2
if [[ ! -f "${system_python}" ]]; then
echo "no system python found"
return 1
fi
pushd ${pd}
if [[ ! -f virtualenv.py ]]; then
curl -L -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py
fi
if [[ ! -d ${env_name} ]]; then
echo "installing virtualenv"
${system_python} virtualenv.py ${env_name} --no-setuptools
if [[ ! -f get-pip.py ]]; then
curl -L -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
fi
${env_name}/bin/python ./get-pip.py
fi
echo "switching to existing Python environment ${env_name}"
source "${env_name}/bin/activate"
popd
}
function pve2() {
local system_python
if [[ -f `which python2` ]]; then
system_python=`which python2`
elif [[ -f `which python2.7` ]]; then
system_python=`which python2.7`
elif [[ -f `which python2.6` ]]; then
system_python=`which python2.6`
else
echo "no system python2 found"
return 1
fi
pve "$1" "${system_python}"
}
function pve3() {
local system_python
if [[ -f `which python3` ]]; then
system_python=`which python3`
elif [[ -f `which python3.5` ]]; then
system_python=`which python3.5`
else
echo "no system python3 found"
return 1
fi
pve "$1" "${system_python}"
}
@gcv
Copy link
Author

gcv commented Oct 27, 2015

Copy these functions into your shell startup script: .zshrc or .bash_profile.

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