So, this one was a doozy. I am putting in a workaround, but just putting this out in case there is a better solution out there. And, having spent several hours before figuring this out, I'm also putting this out as a gotcha.
Basically, I was wondering if there are clever ways to avoid recursive calls to django.setup().
I have 3 or 4 batch scripts that I can run in either standalone mode or from celery. One of them is called build_profiles.py
The way celery gets to see them (in one of the tasks.py) files:
from pssecurity.batch.build_profiles import \
ProfileManager as MgrCls_profiles, \
getOptParser as getOptParser_profiles
In Django 1.6 this arrangement worked fine (I am not totally convinced celery is the best way to launch potentially stand-alone processes but that's another story).
When I tried to run build_profiles.py from the command line, it gave an AppRegistryNotReady error.
No problem, I thought, let's add the following to the top of build_profiles.py, as per https://docs.djangoproject.com/en/dev/ref/applications/#applications-troubleshooting
import django
django.setup()
And then nothing was working anymore with Django. unit tests would not run, manager.py runserver would hang. How could a change to a stand alone batch bring my system to a halt?
Turns out that django.setup() discovers celery which loads its tasks, and if one of those ends up doing its own django.setup()...