Sphinx:Autodoc ImportError

时间:2014-08-28 21:39:56

标签: django python-sphinx

我正在尝试为我的Django项目自动生成文档。 doctree是在html页面上生成的,但是我的任何模块中都没有任何成员的文档。我一直遇到这个问题:

Running Sphinx v1.1.3
loading pickled environment... not yet created
building [html]: targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed 
Traceback (most recent call last):                                                            
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/admin.py", line 1, in <module>
from tool.models import All_alerts
File "/home/tim/Desktop/fqdn/mysite/tool/models.py", line 3, in <module>
from django.db import models
ImportError: No module named django.db
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/forms.py", line 1, in <module>
from django import forms
ImportError: No module named django
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/models.py", line 3, in <module>
from django.db import models
ImportError: No module named django.db
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/page1urls.py", line 1, in <module>
from django.conf.urls import patterns, url, include
ImportError: No module named django.conf.urls
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/page2urls.py", line 1, in <module>
from django.conf.urls import patterns, url, include
ImportError: No module named django.conf.urls
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/page3urls.py", line 1, in <module>
from django.conf.urls import patterns, url, include
ImportError: No module named django.conf.urls
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/page4urls.py", line 1, in <module>
from django.conf.urls import patterns, url, include
ImportError: No module named django.conf.urls
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/tests.py", line 1, in <module>
from django.test import TestCase
ImportError: No module named django.test
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/urls.py", line 1, in <module>
from django.conf.urls import patterns, url, include
ImportError: No module named django.conf.urls
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.py", line 321, in import_object
__import__(self.modname)
File "/home/tim/Desktop/fqdn/mysite/tool/views.py", line 1, in <module>
from tool.models import Product
File "/home/tim/Desktop/fqdn/mysite/tool/models.py", line 3, in <module>
from django.db import models
ImportError: No module named django.db

/home/tim/Desktop/fqdn/mysite/docs/tool.rst:7: WARNING: autodoc can't import/find module 'tool.admin', it reported error: "No module named django.db", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:15: WARNING: autodoc can't import/find module 'tool.forms', it reported error: "No module named django", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:23: WARNING: autodoc can't import/find module 'tool.models', it reported error: "No module named django.db", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:31: WARNING: autodoc can't import/find module 'tool.page1urls', it reported error: "No module named django.conf.urls", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:39: WARNING: autodoc can't import/find module 'tool.page2urls', it reported error: "No module named django.conf.urls", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:47: WARNING: autodoc can't import/find module 'tool.page3urls', it reported error: "No module named django.conf.urls", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:55: WARNING: autodoc can't import/find module 'tool.page4urls', it reported error: "No module named django.conf.urls", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:63: WARNING: autodoc can't import/find module 'tool.tests', it reported error: "No module named django.test", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:71: WARNING: autodoc can't import/find module 'tool.urls', it reported error: "No module named django.conf.urls", please check your spelling and sys.path
/home/tim/Desktop/fqdn/mysite/docs/tool.rst:79: WARNING: autodoc can't import/find module 'tool.views', it reported error: "No module named django.db", please check your spelling and sys.path

我一直在网上寻找任何解决方案。我尝试重新安排模块中的import语句,但没有任何工作。

sys.path.insert(0, os.path.abspath('/home/tim/Desktop/fqdn/mysite/tool/'))

我的所有模块都在工具文件夹中。迫切需要任何帮助。谢谢!

另外,自动模块不封装自动功能吗?那么我们为什么要使用autofunction?

1 个答案:

答案 0 :(得分:0)

您应该使用MagicMock来简化依赖关系。

conf.py

from unittest.mock import MagicMock
import sys

MODULES = ['django']
for mod_name in MODULES:
    sys.modules[mod_name] = MagicMock()