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 28752 Prevent django.setup() from running multiple times pascal chambon pascal chambon "I've been bitten numerous times by the impredictable behaviour of django when django.setup() was called numerous times. In the old days I had exceptions, now it's mainly subtle breakages of logging configuration. I couldn't find, in the issue tracker or the dev mailing list statements about this subject, others than request from other users encountering the problem. For example #26152 concerned script+importable modules. The latest case in date for me is pytest-django having troubles with these multiple setup() calls : https://github.com/pytest-dev/pytest-django/issues/531 , due to multiple fixtures attempting this auto-setup. Would it be OK to make django.setup() idempotent, or even expose an ""is_ready"" flag for easier introspection ? -- here are some updates, comments get rejected as spam -- Calling django.setup() multiple times is useless, BUT it can happen in lots of cases, that's why imho this case should be handled by the framework to avoid nasty side effects. These ""duplicate calls"" often involve the collision between manage.py commands, tests, custom scripts, and external launchers like pytest-django. Plus maybe some corner cases when unittest-style TestCases and pytest-style test functions are mixed in the same project. Users have to do a real gym to call setup() ""at some moment"" in all these use cases, yet try to prevent multiple calls of this initialization step (like the `if__name__ == ""main""'` protection). So far my only way out was often to check for (not really undocumented) states of the framework before calling setup()." Cleanup/optimization closed Core (Other) 1.11 Normal Aymeric Augustin Accepted 1 0 0 0 0 0