Django cron on Webfaction

James Bennett addresses one of the most frequently asked questions in Django – “How do I write a standalone script which makes use of Django components?”

That is what I needed to do. I’m still learning Python so I wasn’t sure why the methods he described in his article didn’t work for me. (OK, I have an idea, but for the fear of looking stupid I’m not going to try to explain it.)

I’m using Webfaction to host my site, so I turned to their forum for assistance and found this topic. Still the examples didn’t quite work for me either. Finally I found a clue in my WSGI script. Adding the following lines to my python file is what I needed to get me going:

import sys, os
sys.path = ['/home/mylogin/webapps/mysite, '/home/mylogin/webapps/mysite/lib/python2.5'] + sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = ‘mysite.settings’

Related posts:

  1. Basic Authentication on mod_wsgi I’m currently in the process of creating an iPhone app...
  2. Webfaction Review I was looking at my web stats and noticed I...
  3. Django & Djson… er, JSON I must admin this is my first attempt at even...
  4. One Click Django Project Publishing I was reading The Joel Test and I got to...
  5. Mobile App on Subdomain with Django I’ve noticed a fairly common pattern arising with mobile and...

Related posts brought to you by Yet Another Related Posts Plugin.

July 31, 2009  Tags: , , ,   Posted in: Programming & Internet

11 Responses

  1. Mario Adam - July 31, 2009

    For your interest – another approach can be found at djangosnippets.org:

    http://www.djangosnippets.org/snippets/374/

    I use this snippet at several scripts. Simply call it with:

    [code]
    bootstrap('/full/path/to/my/djangoproject/')
    [/code]

    Regards
    Mario

  2. No' - July 31, 2009

    I find it a *lot* more efficient to use custom commands

    http://docs.djangoproject.com/en/dev/howto/custom-management-commands/

    you may use the following command to run via a cron

    python manage.py my-custom-command

  3. Dustin - July 31, 2009

    No’: Thanks for the tip. I wasn’t aware of custom commands. How do you mean a *lot* more efficient? Does it run faster or less memory, or just more convenient/standardized?

  4. Kamu - July 31, 2009

    Hullo!

    I wonder if you have found this already? http://code.google.com/p/django-command-extensions/

    The commands of interest to you in this context would be ‘runscript’ and the ‘runjobs’ (which is designed to be used with cron as seen in http://code.google.com/p/django-command-extensions/wiki/JobsScheduling )

    I hope this is of some help.

    Kamu

  5. No' - August 1, 2009

    > How do you mean a *lot* more efficient? Does it run faster or less memory, or just more convenient/standardized?

    a – your sys.path + os.environ changes are a very ugly hack. yes, it works, but it’s quite un-pythonic.

    b – this hack is not portable. if you want to run the same job in 2 different projects, you’ll have to edit the sys.path items. And if you improve your source script, you’ll see it’s pretty painful to keep all your copies up to date.

    c – custom commands are classes. And as every python class, you can import / extend / inherit them. I don’t want to argue, but Object-oriented is far better from a programmer perspective. less code, more leverage.

    d – these classes have helpers to add options, and extra-arguments. say you want to make a daily, weekly, monthly backup of your database via a cron. Will you write 3 different scripts? no, you’ll build one with the appropriate argument. And you won’t have to reinvent the argument parsing, etc. it’s all done very easily.

  6. No' - August 1, 2009

    oh, and I must add… when you start using custom commands, it looks like a hammer, and every problem looks like a nail. ;op

  7. pytechd - August 1, 2009

    The article from James was written prior to custom manage.py commands; as was the “runscript” patch.

  8. Jj - August 1, 2009

    +1 for django-command-extensions.

    Be aware that the article dates back from 2007, lots of django versions in the past.

  9. Urlaub Sardinien - August 7, 2009

    I also had problem with Django before but the custom commands helps me better, like what “No” said, for me it is more efficient and easy to understand.

  10. Verkaufswagen - January 5, 2010

    Be aware that the article dates back from 2007, lots of django versions in the past.

  11. Peter from Home Computers - January 11, 2010

    Using custom commands with Django helped me in many issues. It’s really like that hammer and nails allegory.

Leave a Reply