This was originally the majority of this post: Start a django project the right way but I've decided to put in it's own post instead.
I'm assuming you're on OSX. If you're on Linux most of these steps will be the same. If you're on Windows… Install VMWare and then install Linux.
I've added a tl;dr
at the end.
Although I have tweaked this over the years, the original code came from (I think, I've long since lost the bookmark) klymyshyn.com
Get python working properly
Use the newest version of python from homebrew.
- install homebrew
- install virtualenvwrapper
brew install python
easy_install pip
pip install virtualenv virtualenvwrapper ipython
This will create your virtualenv directory and setup your environment for the first time.
source `which virtualenvwrapper.sh`
Get your shell working for you
Put the following at the end of your ~/.bashrc
export WORKON_HOME=~/.virtualenvs
source `which virtualenvwrapper.sh`
export PREVPWD=`pwd`
export PREVENV_PATH=
handle_virtualenv(){
if [ "$PWD" == "$PREVPWD" ]; then
return;
fi
# deactivate virtualenv if applicable
PREVPWD="$PWD";
if [ -n "$PREVENV_PATH" ]; then
if [ "`echo "$PWD" | grep -c $PREVENV_PATH`" = "0" ]; then
deactivate
unset venv_ps1
PREVENV_PATH=
fi
fi
# activate virtualenv if applicable
if [ -e "$PWD/.venv" ] && [ "$PWD" != "$PREVENV_PATH" ]; then
PREVENV_PATH="$PWD"
# you can put some exports in .venv to setup your environment
# eg. DJANGO_SETTINGS_MODULE, PYTHONDONTWRITEBYTECODE, PYTHONPATH, PYTHONSTARTUP
# but they aren't unset when you leave this directory
#
# two likely variables to set are VENV and VDIR. VENV is by default
# the current directory or set VDIR to use regular virtual env and not
# a wrapper
source $PWD/.venv
if [ -e "$VDIR/bin/activate" ]; then
source $VDIR/bin/activate
export venv_ps1=" venv:$VDIR"
return
fi
if [ -z $VENV ]; then
VENV=`basename $PWD`
fi
if [ -n $VENV ]; then
if workon $VENV; then
export venv_ps1=" venv:$VENV"
fi
else
echo "error: not activating a virtual environment"
fi
unset VENV
unset VDIR
fi
}
# this function gets called every time the prompt is shown
function prompt_command_collection() {
handle_virtualenv
# this will change your prompt so you can see what's going on
# comment out if you like your current prompt (give this a try though)
export PS1="[\u@\h \[\e[1;34m\]\w\[\e[0m\]${venv_ps1}]\n\\$ "
}
export PROMPT_COMMAND=prompt_command_collection
run: source ~/.bashrc
in all your open terminals or reopen all your terminals.
Make your project directory
I'm assuming that you're going through all this pain to make a django project but in reality this works for any python project.
By default this script will try to activate a virtual environment with the same name as the current directory. ie: if the file is ~/src/youtube_killer/.venv
then the script will try to activate a virtual environment called youtube_killer.
If you want to activate a different environment, then put the name in .venv. eg. echo other_name > .venv
. This is useful if you have several clones of a git repo that all use the same environment or if you like to use a generic environment with some standard installed packages.
I put all my projects under ~/src/
.
The is the actual command to create a virtual environment.
mkvirtualenv --no-site-packages project_name
And here is how to do it with some visual feedback.
mkvirtualenv --no-site-packages project_name
deactivate # this makes it more fun later
cd
mkdir -p src/project_name
touch project_name/.venv
cd src/project_name # you should now be using virtenv automatically
which python
cd .. # stop using virtenv automatically
which python
And that is how you magically use virtual environments.
tl;dr
# copy bash functions into .bashrc
sudo easy_install pip
pip install virtualenv virtualenvwrapper
source `which virtualenvwrapper.sh`
echo "source `which virtualenvwrapper.sh`" >> ~/.bashrc
mkvirtualenv project_name
deactivate
mkdir project_name
touch project_name/.venv
cd project_name # voila