I have written a few command-line applications using a method similar to your first option. I prefer to do it this way as opposed to using the DJANGO_SETTINGS_MODULE
environment variable because it feels more like a regular Python program (to me).
You should also note that you do not have to put your module in the same directory as your settings.py
; you can use the absolute Python path of your settings module:
from django.core.management import setup_environ
from project import settings
setup_environ(settings)
#The rest of your imports
PEP 8 discourages relative imports anyway.
I always install my Django applications in site-packages (/usr/lib64/python2.6/site-packages
on Gentoo) so I don't have to worry about setting PYTHONPATH
from my crontabs, but I don't believe that is a widely practiced method. I also like to use setuptools Automatic Script Creation so that my console scripts get put wherever they should be (/usr/bin
, for example) and are named appropriately automatically. Your first option also facilitates this.