All Articles Kinsa Creative

Access Django within a Python Script

A Python file needs to set the DJANGO_SETTINGS_MODULE environmental variable before importing Django and setting it up. In a new file, import os and then set the environmental variable to point to the settings file on the Python path:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings.development'

import django

You can now import your project models, views, or any other code you wish to access and you can execute the script from the shell:

$ python


Email us at