Oskar Persson
Oskar Persson

Reputation: 6765

Sphinx fails when generating documentation for Django project

I'm trying to automatically generate documentation for my Django project using Sphinx with the autodoc and napoleon extensions.

Using sphinx-quickstart I've created the following structure:

MyDjangoProject
├── __init__.py
├── config
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── docs
│   ├── Makefile
│   ├── build
│   └── source
│       ├── _static
│       ├── _templates
│       ├── conf.py
│       └── index.rst
├── myfirstapp
│   ├── __init__.py
│   ├── models.py
│   └── views.py
├── mysecondapp
│   ├── __init__.py
│   ├── models.py
│   └── views.py

...

I've customized docs/source/conf.py to reflect my project structure.

import os
import sys

proj_folder = os.path.realpath(
    os.path.join(os.path.dirname(__file__), '../..'))

sys.path.append(proj_folder)

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

import django
django.setup()

extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx.ext.napoleon', 'sphinx.ext.viewcode']

# The rest of the default configuration...

Then I go to the root of my project and run sphinx-apidoc -f -o docs/source .. This adds a .rst file for each module to docs/source.

Finally I go to MyDjangoProject and run make html. This fails with an error for each module saying

Traceback (most recent call last):
  File "/Users/Oskar/git/MyDjangoProject/venv/lib/python2.7/site-packages/sphinx/ext/autodoc.py", line 551, in import_object
    __import__(self.modname)
ImportError: No module named MyDjangoProject.myfirstapp

What am I doing wrong?

Upvotes: 1

Views: 436

Answers (1)

Alasdair
Alasdair

Reputation: 309099

Since you have added MyDjangoProject to the python path, you should import myfirstapp as myfirstapp instead of MyDjangoProject.myfirstapp.

Upvotes: 2

Related Questions