Documenting a Django project with Sphinx

Sphinx is a documentation tool written for python (but could be used
on other circumstances). I’ve learning how to used and I wanted to
document a django project I’ve been working on, but when you try to
import your modules you get an exception

 autodoc can't import/find module '<yourmodule>', it reported error:
 "Settings cannot be imported, because environment variable
 DJANGO_SETTINGS_MODULE is undefined.",please check your spelling
 and sys.path

This happens because django do some enviroment settings before using
your app, so I wanted to do the same with. It was easier than I
tought, just put the following snippet in your sphinx conf.py

from MYPROJECT import settings
from django.core.management import setup_environ

setup_environ(settings)

of course, for it to work you need to modify your path, in my case was:

sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../../'))

Comments are currently closed.