Skip to content

Instantly share code, notes, and snippets.

@TaurusOlson
Last active June 30, 2016 06:00
Show Gist options
  • Save TaurusOlson/35feae2a7552b6f29edece1c09cd9d9e to your computer and use it in GitHub Desktop.
Save TaurusOlson/35feae2a7552b6f29edece1c09cd9d9e to your computer and use it in GitHub Desktop.
A bash sript creating a temporary Python virtual environment with optional packages.
#! /bin/bash
function usage() {
cat <<EOF
Create a temporary Python virtual environment with optional packages.
Usage:
$(basename $0) [package1 package2...]
EOF
}
if [ $# -eq 1 ]; then
[ $1 = '-h' ] || [ $1 = '--help' ] && usage && exit 1
fi
venv_name=venv_temp_$(date +'%Y%m%d%H%M')
echo -e "\n* Creating the virtual environment $venv_name...\n"
virtualenv $venv_name
source $venv_name/bin/activate
# Install ipython
pip install -q ipython
packages="$@"
for package in $packages; do
echo -e "\n* Installing $package..."
pip install -q $package
[ $? -eq 0 ] && echo "> Succeded"
[ $? -eq 1 ] && echo "> Failed"
done
$venv_name/bin/ipython
echo -e "\n* Removing the virtual environment $venv_name.\n"
rm -r $venv_name
@TaurusOlson
Copy link
Author

TaurusOlson commented Jun 28, 2016

venv_temp.sh

creates a virtual environment named venv_temp_YYYYMMDDhhmm
where YYYYMMDDhhmm designates the current date and time.

venv_temp.sh requests records

creates a virtual environment named venv_temp_YYYYMMDDhhmm and installs the packages requests and records.

After the installation, ipython is launched. When you're done, CTRL-d or exit will exit ipython and remove the virtual environment.

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