首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sphinx“警告: autodoc:导入类失败”错误

Sphinx“警告: autodoc:导入类失败”错误
EN

Stack Overflow用户
提问于 2019-09-12 18:07:43
回答 1查看 1.8K关注 0票数 1

我正在尝试使用Sphinx为一些python类生成文档,我有一个带有一个类的python脚本(带有一些文档)

我的python脚本在这里: D:\sphynx\scripts\src\test.py

我不确定这是否正确,但这是我的test.rst

代码语言:javascript
复制
.. automodule:: src
Base Class
----------
.. autoclass:: Test
   :members:

modules.rst

代码语言:javascript
复制
scripts
=======

.. toctree::
   :maxdepth: 4

   test
代码语言:javascript
复制
class Test(object):
    """ test docs 
    """
    def __init__(self, **kwargs):
        """Initialize a Test object.
        """
        self._params = kwargs
    def my_method(self, param):
        """ method docs here
        Args:
            param: Input param.
        Returns:
            Test: Returns self.
        """
        return self

index.rst

代码语言:javascript
复制
.. Test documentation master file, created by
   sphinx-quickstart on Mon Sep  9 11:45:25 2019.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to Pets's documentation!
================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   modules

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

conf.py

代码语言:javascript
复制
import os
import sys
sys.path.insert(0, os.path.abspath('D:/sphynx/scripts'))

extensions = ['sphinx.ext.autodoc']

一旦我运行'make html‘命令,我就会得到这个错误:

代码语言:javascript
复制
D:\sphynx>make html
...
reading sources... [100%] test
WARNING: autodoc: failed to import class 'Test' from module 'src'; the following exception was raised:
Traceback (most recent call last):
  File "c:\anaconda3\lib\site-packages\sphinx\util\inspect.py", line 230, in safe_getattr
    return getattr(obj, name, *defargs)
AttributeError: module 'src' has no attribute 'Test'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\anaconda3\lib\site-packages\sphinx\ext\autodoc\importer.py", line 71, in import_object
    obj = attrgetter(obj, attrname)
  File "c:\anaconda3\lib\site-packages\sphinx\ext\autodoc\__init__.py", line 226, in get_attr
    return autodoc_attrgetter(self.env.app, obj, name, *defargs)
  File "c:\anaconda3\lib\site-packages\sphinx\ext\autodoc\__init__.py", line 1501, in autodoc_attrgetter
    return safe_getattr(obj, name, *defargs)
  File "c:\anaconda3\lib\site-packages\sphinx\util\inspect.py", line 246, in safe_getattr
    raise AttributeError(name)
AttributeError: Test

looking for now-outdated files... none found

我错过了什么?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2019-09-13 01:14:42

没有名为src的模块。它只是一个文件夹。Test类在该文件夹中的test模块(test.py)中定义。

使用conf.py中的sys.path.insert(0, os.path.abspath('D:/sphynx/scripts/src')).. automodule:: test应该可以为您工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57904413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档