I want to write documents with sphinx on my django project and auto create docs with my django code comments.
Now I have a django(1.9) project, the file structure is as below:
myproject/
myproject/
__init__.py
settings.py
urls.py
wsgi.py
myapp/
migrations/
__init__.py
admin.py
models.py
tests.py
views.py
docs/
_build/
_static/
_templates/
conf.py
index.rst
Makefile
Then, as you see, I put a docs
folder which holds a Sphinx doc project inside.
Now I can edit the *.rst
files and build html
.
But when I tried to autodoc
the contents, it fails.
Below is what I did:
First, I added these to the docs/conf.py
, ref: https://stackoverflow.com/a/12969839/2544762:
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
Then, I made a sphinx-apidoc action:
sphinx-apidoc -o docs/documentation .
After that, in the docs/documentations
, I got some .rst
files:
myproject/
docs/
documentations/
myapp.rst
myapp.migrations.rst
myproject.rst
manage.rst
modules.rst
After that, I run make html
, and have the waring with:
sphinx-build -b html -d _build/doctrees . _build/html
Running Sphinx v1.3.3
loading translations [zh_CN]... done
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 3 source files that are out of date
updating environment: 2 added, 3 changed, 0 removed
reading sources... [100%] documentation/modules
/home/alfred/app/myproject/docs/documentation/core.rst:25: WARNING: autodoc: failed to import module 'core.models'; the following exception was raised:
Traceback (most recent call last):
File "/home/alfred/.local/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 385, in import_object
__import__(self.modname)
File "/home/alfred/app/myproject/myapp/models.py", line 4, in <module>
from django.contrib.auth.models import User, Group
File "/home/alfred/.local/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/alfred/.local/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/home/alfred/.local/lib/python3.5/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/alfred/.local/lib/python3.5/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/home/alfred/.local/lib/python3.5/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
/home/alfred/app/myproject/docs/documentation/core.migrations.rst:10: WARNING: autodoc: failed to import module 'core.migrations.0001_initial'; the following exception was raised:
Traceback (most recent call last):
File "/home/alfred/.local/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 385, in import_object
__import__(self.modname)
File "/home/alfred/app/myproject/myapp/migrations/0001_initial.py", line 7, in <module>
import django.contrib.auth.models
File "/home/alfred/.local/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/alfred/.local/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/home/alfred/.local/lib/python3.5/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/alfred/.local/lib/python3.5/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/home/alfred/.local/lib/python3.5/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
/home/alfred/app/myproject/docs/documentation/myproject.rst:10: WARNING: invalid signature for automodule ('myproject.settings-sample')
/home/alfred/app/myproject/docs/documentation/myproject.rst:10: WARNING: don't know which module to import for autodocumenting 'myproject.settings-sample' (try placing a "module" or "currentmodule" directive in the document, or giving an explicit module name)
/home/alfred/app/myproject/docs/documentation/myproject.rst:26: WARNING: autodoc: failed to import module 'myproject.urls'; the following exception was raised:
Traceback (most recent call last):
File "/home/alfred/.local/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 385, in import_object
__import__(self.modname)
File "/home/alfred/app/myproject/myproject/urls.py", line 20, in <module>
url(r'^admin/', include(admin.site.urls)),
File "/home/alfred/.local/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 303, in urls
return self.get_urls(), 'admin', self.name
File "/home/alfred/.local/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 258, in get_urls
from django.contrib.contenttypes import views as contenttype_views
File "/home/alfred/.local/lib/python3.5/site-packages/django/contrib/contenttypes/views.py", line 5, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/alfred/.local/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
class ContentType(models.Model):
File "/home/alfred/.local/lib/python3.5/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/alfred/.local/lib/python3.5/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/home/alfred/.local/lib/python3.5/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
looking for now-outdated files... none found
pickling environment... done
checking consistency... /home/alfred/app/myproject/docs/documentation/modules.rst:: WARNING: document isn't included in any toctree
done
preparing documents... done
writing output... [100%] index
generating indices... genindex py-modindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded, 6 warnings.
Build finished. The HTML pages are in _build/html.
What did I do wrong? How can I build the document with the django code?
Long time to find the solution:
In the conf.py
, add the following:
import django
sys.path.insert(0, os.path.abspath('..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
django.setup()