Problem
While trying to generate sphinx documentation for a re-usable Django application I struck upon the following snafoo. When sphinx parses the model.py
code it is thwarted by the code therein trying to access the Django project settings. As this is a standalone/reusable application there is not a primary project providing these settings i.e. there is no ROOT/PROJECT/PROJECT/settings.py
file.
Setup
In the interest of clarity here is what I've done. Traverse to what would be the usual project folder cd ROOT/PROJECT
and create an application django-admin startapp APPLICATION
which produces the following structure
/ROOT/
/PROJECT/
/APPLICATION/
admin.py
apps.py
models.py
tests.py
views.py
Note : There are no /ROOT/PROJECT/PROJECT/*.py
files because I did not navigate to the root folder cd root
and create a project using django-admin createproject
as one normally might.
Next one creates the sphinx documentation spinx-quickstart docs
producing the following additional structure.
/ROOT/
/PROJECT/
/docs/
/source/
...
conf.py
make.bat
That is the docs are built next to the APPLICATION
.
Question
What do I place within conf.py
to properly load the application without there being a settings.py
file ?
Homework
In trying to resolve this I have perused a number of SO Questions, Blogs and the Django Docs and not found a succinct solution. As this has been asked a few times before on SO I'd like to motivate that it not be closed as a duplicate, if the answer in the proposed duplicate uses one of these snippets as it's solution.
Fails with
AppRegistryNotReady
from django.conf import settings settings.configure()
Similar to the first method of failure i.e. emits
AppRegistryNotReady
from django.conf import settings settings.configure() settings.INSTALLED_APPS += ['fintech']
Fails with
ImproperlyConfigured
import django django.setup()
There is a really old solution mentioning the deprecated
setup_environ
from django.core.management import setup_environ from django.conf import settings settings.configure() setup_environ(settings)
This is also a favourite answer but fails if there is no
settings.py
file.import django os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECT.settings' django.setup()
I've also made this question rather verbose as the related questions on SO are rather terse and not especially helpful. If it's any help I'm using Django 1.10.
Update
I have since found that if one imports their setup
function from setuptools
versus distutils.core
that one may invoke the setup script to compile their documentation as in python setup.py build_sphinx -b BUILDER
It's probably best to re-ask this when invoking setup.py
over docs/conf.py
via either the make.bat
or the MakeFile
provided by Sphinx.
I suspect the outcome would be similar though, namely include the provided answers within docs/conf.py
or alternatively within setup.py
, both must be called within the same Python session after all.
settings.py
. If your app expect some global settings, I think you should define them (as default values) in app related settings.settings.py
, but Sphinx couldn't load themodels.py
module to read the docstrings because it importsdjango.db.models
and that throws:Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
settings.configure()
anddjango.setup()
from any script that is loaded prior to the loading of amodels.py
module. Django per se does not seem to care where one does this. Thanks all for your time and contributions. I tried contributing back in the update, If I must elaborate on this let me know.