08 November 2012
mkdjangovirtualenv: an easier way to setup a virtualenv for a django project
The problem
Lately my process for getting a django project setup in a new environment has looked like:
- make sure pip, virtualenv, and virtualenvwrapper are installed
- assuming it’s a git repo—clone the project
- cd into the project
- mkvirtualenv -a
pwd
-r requirements.txt project_name - set PYTHONPATH and DJANGO_SETTINGS_PROJECT inside ~/.virtualenvs/project_name/bin/postactivate
- init and update git submodules
I often forget to add the extra arguments for step 3, step 4 is busywork, and step 5 is something I generally don’t run into until my project fails to find code from a submodule.
The solution
I wrote a script to handle steps 3–6. You can install it from PyPI with pip or easy_intall:
sudo pip install mkdjangovirtualenv
For best results, you should also source mkdjangovirtualenv.sh
in your startup script (e.g., ~/.bash_profile, ~/.profile). You can find where the script is located by running which mkdjangovirtualenv.sh
:
source /usr/local/bin/mkdjangovirtualenv.sh
Usage:
mkdjangovirtualenv [--settings DJANGO_SETTINGS_MODULE] ENV_NAME [PROJECT_DIR]
For example, to make an env named "radical" for a project in the directory "cowabunga":
mkdjangovirtualenv radical cowabunga/
If you don’t give a path at the end, it will use whatever your current working directory is as the default project path.
The django settings module defaults to "project.settings".* Change it with the --settings
flag. Here’s how to make an env named “monkey_business” that has a settings module of “monkeys.settings”:
mkdjangovirtualenv --settings monkeys.settings monkey_business
More info and source code can be found on the github page for this project. Give it a try, let me know if it’s useful for you or if you run into issues.
Related Posts:
Tips for using pip + virtualenv + virtualenvwrapper
A menu shortcut for activating virtualenvs