Execute a standalone Python script after having set up the Django environment. run is almost the same as redirecting stdin of Django's shell command (i.e. doing shell <, but with the possibility of using command line arguments.

For example if you have a file with the following content...

import sys
from myapp.models import Partner
print Partner.objects.get(pk=sys.args[1])

... then you can run this script using:

$ python run 101
Bäckerei Ausdemwald

This command modifies sys.args, __file__ and __name__ so that the invoked script sees them as if it had been called directly.

It is similar to the runscript command which comes with django-extensions.

This is yet another answer to the frequently asked Django question about how to run standalone Django scripts ([1], [2]).

(This module's source code is available here.)


Command([stdout, stderr, no_color, force_color])