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:

  1. make sure pip, virtualenv, and virtualenvwrapper are installed
  2. assuming it’s a git repo—clone the project
  3. cd into the project
  4. mkvirtualenv -a pwd -r requirements.txt project_name
  5. set PYTHONPATH and DJANGO_SETTINGS_PROJECT inside ~/.virtualenvs/project_name/bin/postactivate
  6. 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.

*I like to name my django project’s "project", since they’d often otherwise have a redundant name to their parent folders.


Related Posts:
Tips for using pip + virtualenv + virtualenvwrapper
A menu shortcut for activating virtualenvs




Interested in working with me at Superset to help shape the future of virtual fitness and to empower people to launch their own businesses online? We’re hiring a full-stack engineer. 💪🤓💻 Apply online



Did you find this helpful or fun? paypal.me/mrcoles
comments powered by Disqus

Peter Coles

Peter Coles

is a software engineer living in NYC who is building Superset 💪 and also created GoFullPage 📸
more »

github · soundcloud · @lethys · rss