Back to writing

Article

Running a Standalone Script in Django

A standalone Python module can use Django models and services after Django’s settings and app registry have been initialized. Set DJANGO_SETTINGS_MODULE to the project settings module, call django.setup(), and execute the script as a module from the project root.

1) write in terminal:

export DJANGO_SETTINGS_MODULE=mysite.settings

 

2) use this code first line of script:

import django django.setup()

 

3) run scriprt:

python -m app.script