﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26152	django.setup() hangs django if ever used in a module that's imported by an app	Harry Percival	nobody	"The docs suggest using django.setup() for any script that needs to be run standalone:

https://docs.djangoproject.com/en/1.9/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage

But this causes problems if a standalone script also contains some code that is imported by a regular django app.

Here's a minimal repro:

https://github.com/hjwp/recursive_django_setup_bug_minimal_repro/commit/15208c126796980cbe238ffe168d8b1bafaace26

if foo.py contains django.setup(), and myapp.models.py tries to import from foo.py, that now causes a hang such that no manage.py call will ever complete.  ouch!

The solution/workaround is to put any calls to django.setup() inside a 

{{{
if __name__ == '__main__':
    import django
    django.setup()
}}}

I gather that fixing django.setup() / apps.populate() to make it reentrant is hard.  So this could be a quick documentation fix instead?

cf #18251, a more complex version of this bug I think, which is in fact quite simple to run into..."	Cleanup/optimization	closed	Documentation	1.9	Normal	fixed			Accepted	0	0	0	0	0	0
