How to call django.setup() in console_script?

had the same problem (or something similar). I solved it by doing:

[Warning: dirty solution]

if not hasattr(django, 'apps'):
    django.setup()

this way it'll be called only once even if it's imported multiple times


Here https://docs.djangoproject.com/en/1.10/_modules/django/#setup we can see what django.setup actually does.

Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if set_prefix is True.

So basically to ensure that setup was already done we can check if apps are ready and settings are configured

from django.apps import apps
from django.conf import settings

if not apps.ready and not settings.configured:
    django.setup()