use django: from “python shell” to python script

Each Answer to this Q is separated by one/two green lines.

I can move to a python project directory (say c:\www\myproject) and then issue

   python shell

and then I can use all modules from django project, say the following piece of commands from the shell command:

import settings 
from django.template import Template, Context

t=Template("My name is {myname}.")
f = open('write_test.txt', 'w')

now, when I tried to collect all my commands into a python script, say “”, I cannot execute the script. I must missed something important.

I issued python

then I got Import error: could not import settings Is it on sys path?”

I’m in the project directory where resides…..

Could some one help me out?


Try using a Django management command instead.

# myproject/myapp/management/commands/

from import NoArgsCommand
from django.template import Template, Context
from django.conf import settings

class Command(NoArgsCommand):
    def handle_noargs(self, **options):
        t=Template("My name is {myname}.")
        f = open('write_test.txt', 'w')

And then (if you follow the docs) you will be able to execute the command in the following fashion:

python my_command

This method is deprecated in Django 1.4. Use django.conf.settings.configure() instead
(see @adiew’s answer for example code).

Old method follows.

Put this at the beginning of your script

from import setup_environ
import settings

This is really what the does behind the scene. To see it view the Django source in django/core/management/ After executing these lines everything should be just like in ./ shell.

Try put these two lines at the beginning of your script:

from django.conf import settings
settings.configure() # check django source for more detail

# now you can import other django modules
from django.template import Template, Context

UPDATE: From another post.

./ shell <

To import Django’s settings use:

from django.conf import settings

Instead of manually adding things to your python script, or having to fit in the management command format, in case this is not something that needs to stay around long, you can get all the benefits of the Django environment by running your script with ./ runscript <>

… but if your script is in your project folder, then you can just add this line to the top of the python script: import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

Saw in a good solution for Django >= 1.7 and in case someone uses django-configurations this worked for me:

import sys, os, django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.local") # path to config

## if using django-configurations
os.environ.setdefault("DJANGO_CONFIGURATION", "Local")
from configurations import importer

django.setup() ## apparently important for django 1.7

from foo.models import Bar

print Bar.objects.all()

Here is yet another variant: I wrote and often use a management command “run” which has the advantage that scripts can see their command-line parameters:

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .