python - Autodoc Sphinx 出现 django 错误

标签 python django python-sphinx autodoc

当我尝试使用 autodoc 制作 html 时出现错误。

这是我的项目结构:

projet
--- app1
------ models.py
------ views.py
--- app2
--- app3
docs
--- modules
------ models.rst
--- conf.py
--- index.rst
__init__.py
manage.py

我的conf.py

import os
import sys

sys.path.insert(0, os.path.abspath('..'))
from django.conf import settings
settings.configure()

import django
django.setup()

# -- General configuration 

extensions = ['sphinx.ext.autodoc',
'sphinx.ext.coverage']

还有我的模型.rst

Models
======
.. automodule:: projet.app1.models
    :members:
    :undoc-members:

我遇到了这个错误:

D:\Agrome\Agrome bitbucket\agrome-backend\docs\modules\models.rst:3: WARNING: autodoc: failed to import module u'agrome.boxes.models'; the following exception was raised:
    Traceback (most recent call last):
        File "c:\python27\lib\site-packages\sphinx\ext\autodoc.py", line 547, in import_object
        __import__(self.modname)
        File "D:\Agrome\Agrome bitbucket\agrome-backend\agrome\boxes\models.py", line 5, in <module>
        from django.contrib.auth.models import User
        File "c:\python27\lib\site-packages\django\contrib\auth\models.py", line 6, in <module>
        from django.contrib.contenttypes.models import ContentType
        File "c:\python27\lib\site-packages\django\contrib\contenttypes\models.py", line 161, in <module>
        class ContentType(models.Model):
        File "c:\python27\lib\site-packages\django\db\models\base.py", line 102, in __new__
        "INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

如果我输入 .. automodule::projet.app1 (没有 .models),它可以工作,但文档是空的。

你有什么想法吗?

非常感谢

最佳答案

尝试删除/注释掉conf.py中的settings.configure()

关于python - Autodoc Sphinx 出现 django 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778423/

相关文章:

django admin显示所有应用程序、模型,但管理模型页面返回404

javascript - Django:如何创建一个随用户点击而变化的动态表单?

css - 使用 CSS 的狮身人面像和 Accordion 列表

python - 通过 Sphinx 从脚本中获取文档字符串

python - 如何调试 Matplotlib 中的散点图?

python - 导入 pytesseract

python - Tensorflow 2.0 意外 OOM

django reverse 导致循环导入

python - Sphinx 文件间链接函数

python - 主键字段上的 serialize=False 是什么意思?