I like to add the following command to my projects:
myproject/management/commands/run-script.py
:
from django.core.management.base import BaseCommand, CommandError
import imp
import sys
class Command(BaseCommand):
help = """Run a non-django script with django settings."""
args = "<path_to_script.py> [<script_args>...]"
def handle(self, *args, **options):
if len(args) == 0:
raise CommandError("Path to script to run is required")
sys.argv = list(args)
imp.load_source("__main__", args[0])
Then, I can run custom scripts, e.g:
./manage.py run-script /path/to/myscript.py --opt=value arg1 arg2