Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Django shell with ability to execute python scripts.
Branch: master
Failed to load latest commit information.


From time to time I use the shell command to launch simple one-time tasks within my Django projects in the non-interactive mode. For example:

$ echo 'from django_app.tasks import procmail; procmail()' | ./ shell

It's possible to run python scripts too:

$ echo 'execfile("/path/to/migrations/")' | ./ shell

It is fast and easy, than write real application management commands. But now I have some restrictions:

  1. Annoying syntax for a command execution.
  2. I can't pass additional command line arguments because shell refuses them:

    $ echo 'import sys; print sys.argv' | ./ shell --foo ... error: no such option: --foo

  3. The shell pollutes both stdin and stdout with banners from interactive python:

    $ echo '' | ./ shell Python 2.5.2 (r252:60911, Jan 20 2010, 23:14:04) [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole)

I suppose the shell command line interface should be similar to regular python.

Should execute python code:

$ ./ shell -c 'from django.db.models.loading import get_models; print get_models()'

Should execute python files:

$ ./ shell /path/to/migrations/

Should execute python modules:

$ ./ shell -m smtpd -n -c DebuggingServer localhost:1025

Should execute python code from stdin:

$ echo 'from django.db.models.loading import get_models; print get_models()' | ./ shell -

Under *nix-like environments it's possible to use shell as interpreter. Just put into the PATH:

#! shell

from django.db.models.loading import get_models, get_apps
print "Models", get_models()
print "Applications", get_apps()

Will, whether the work in this area helpful for Django?

Something went wrong with that request. Please try again.