How to run an arbitrary script in the context of the Django project?

Sep 23, 2013 · Updated: Jul 12, 2021 · by Tim Kamanin

The easiest way to do this is to set up your script as a subcommand. Here's how to do that:

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 ...

Save it as management/commands/ and now you can call your script with ./ yourcommand. Simple!

Example taken from:

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓