Skip to content

Instantly share code, notes, and snippets.

@tysm
Last active October 16, 2019 03:17
Show Gist options
  • Save tysm/d7b230ea9591f0eae71f7f36f88687d0 to your computer and use it in GitHub Desktop.
Save tysm/d7b230ea9591f0eae71f7f36f88687d0 to your computer and use it in GitHub Desktop.
Python Virtual Enviroment setup and sourcer using python-pip and python-venv
#!/bin/bash
SCRIPT_PATH="${BASH_SOURCE[0]#./}"
if [[ ! ${0#./} != $SCRIPT_PATH ]]; then
echo "You cannot run it directly. This file must be sourced *from bash*: source $SCRIPT_PATH PYTHON"
exit 1
fi
if [[ $# -ne 1 ]]; then
echo "Usage: $SCRIPT_PATH PYTHON"
return 1
else
VENVDIR=".venv-$1"
if [[ ! -d $VENVDIR ]]; then
# creates the virtual environment.
# PYTHON -m venv .venv-PYTHON
if ! (
$1 -m venv "$VENVDIR" && \
source "$VENVDIR/bin/activate" && \
pip install -U pip setuptools && \
pip install --no-cache-dir -r requeriments.txt
); then
# removes the created venv.
rm -rf "$VENVDIR"
return 1
fi
fi
# sources the venv previously created.
source "$VENVDIR/bin/activate"
# exports the project path as part of the PYTHONPATH.
source "$(dirname "$SCRIPT_PATH")/pyth"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment