Yevgeni Burshtein
Yevgeni Burshtein

Reputation: 81

Sphinx "WARNING: autodoc: failed to import class" error

I'm trying to use Sphinx to generate docs for some python class I've one python script with one class (with some docs)

My python script is here: D:\sphynx\scripts\src\test.py

Not sure if this is correct , but this is my test.rst

.. automodule:: src
Base Class
----------
.. autoclass:: Test
   :members:

modules.rst

scripts
=======

.. toctree::
   :maxdepth: 4

   test
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

.. 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

import os
import sys
sys.path.insert(0, os.path.abspath('D:/sphynx/scripts'))

extensions = ['sphinx.ext.autodoc']

Once I run 'make html' command, I get this error:

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

What am I missing ?

Thanks in advance!

Upvotes: 1

Views: 3561

Answers (1)

mzjn
mzjn

Reputation: 50947

There is no module named src. It is just a folder. The Test class is defined in the test module (test.py) which is in that folder.

.. automodule:: test should work for you, with sys.path.insert(0, os.path.abspath('D:/sphynx/scripts/src')) in conf.py.

Upvotes: 2

Related Questions