This is how we prefer to setup our environments at CodeZeus for Python and Django development. It uses virtualenv and virtualenvwrapper to contain dependencies and make the workflow more automatic.
Last active
June 12, 2016 23:55
-
-
Save dansackett/4345519e1ac71d726566e1092b82011e to your computer and use it in GitHub Desktop.
Virtualenv and Virtualenvwrapper Setup
These steps should be followed first.
$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ pip install virtualenv virtualenvwrapper
$ mkdir ~/.virtualenvs ~/projects
Copy the following code into the corresponding files below and then when you create a new project add a directory in the root called bin/. I usually add a postactivate and postdeactivate script which will be called when you workon you project.
A typical workflow with this will be:
$ mkproject PROJECT_NAME
$ deactivate
$ workon PROJECT_NAME
$ deactivate
The scripts below allow hooks in the CWD to run giving projects the ability run code before and after starting / ending work on a project.
File: ~/.bashrc
export WORKON_HOME=~/.virtualenvs
export PROJECT_HOME=~/projects
export PIP_VIRTUALENV_BASE=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
File: ~/.virtualenvs/postactivate
#!/bin/bash
# This hook is sourced after every virtualenv is activated.
# source postactivate hook
_HOOK_PATH=bin/postactivate
_PROJECT_FILE=$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME
if [ -s $_PROJECT_FILE ]; then
export _PROJECT_DIR=$(cat $_PROJECT_FILE)
_HOOK=$_PROJECT_DIR/$_HOOK_PATH
[ -f $_HOOK ] && . $_HOOK
fi
File: ~/.virtualenvs/postdeactivate
#!/bin/bash
# This hook is sourced after every virtualenv is deactivated.
# source postdeactivate hook
_HOOK_PATH=bin/postdeactivate
if [ -n "$_PROJECT_DIR" ]; then
_HOOK=$_PROJECT_DIR/$_HOOK_PATH
[ -f $_HOOK ] && . $_HOOK
unset _PROJECT_DIR
fi
File: ~/.virtualenvs/postmkvirtualenv
#!/bin/bash
# This hook is sourced after a new virtualenv is activated.
proj_name=$(echo $VIRTUAL_ENV|awk -F'/' '{print $NF}')
[ ! -d ~/projects/$proj_name ] && mkdir -p ~/projects/$proj_name
add2virtualenv ~/projects/$proj_name
cd ~/projects/$proj_name
File: ~/.virtualenvs/predeactivate
#!/bin/bash
# This hook is sourced before every virtualenv is deactivated.
cd
File: ~/.virtualenvs/prermvirtualenv
#!/bin/bash
# This hook is run before a virtualenv is deleted.
# argument: full path to environment directory
_PROJECT_FILE="$VIRTUALENVWRAPPER_HOOK_DIR/$@/.project"
if [ -f $_PROJECT_FILE ]; then
_PROJECT_DIR=$(cat $_PROJECT_FILE)
read -p "Remove project directory [$_PROJECT_DIR] (y / n): " confirm
if [[ $confirm == "y" ]]; then
rm -r $_PROJECT_DIR
fi
fi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add Variables to the
4_postmkvirtualenv.mdfile so it relies on.bashrc(NVM This has a bug because it loads the VENV too soon)