Running a Python script outside of Django


I have a script which uses the Django ORM features, amongst other external libraries, that I want to run outside of Django (that is, executed from the command-line).

Edit: At the moment, I can launch it by navigating to a URL...

How do I setup the environment for this?

4/14/2010 2:49:57 AM

The easiest way to do this is to set up your script as a subcommand. It's quite easy to do:

from import NoArgsCommand, make_option

class Command(NoArgsCommand):

    help = "Whatever you want to print here"

    option_list = NoArgsCommand.option_list + (
        make_option('--verbose', action='store_true'),

    def handle_noargs(self, **options):
        ... call your script here ...

Put this in a file, in any of your apps under management/commands/ (with empty files in each) and now you can call your script with ./ yourcommand.

10/13/2012 9:42:40 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow