Run standalone python scripts in Django 1.5


Since setup_environ is deprecated in Django 1.5, we can use os.environ to load environment and run standalone python script.

At this example:

1
2
3
4
5
6
7
myproject
    |
    |
    —- myapp
           |
           |
            ——– standalone_scripts_is_here.py

And here is the code

1
2
3
4
5
6
7
8
9
10
11
import os
import sys

# Loading Django Environment for standalone Python scripts
CURRENT = os.path.dirname(__file__)
sys.path.insert(0, os.path.realpath(os.path.join(CURRENT, ‘..’)))
sys.path.insert(1, os.path.realpath(os.path.join(CURRENT, ‘..’, ‘myproject’)))

os.environ[‘DJANGO_SETTINGS_MODULE’] = "myproject.settings"

….

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.