This question is about sphinx-nested-apidoc (not sphinx-apidoc).
After running
sphinx-nested-apidoc -o docs/source mymodule/
I am having a structure as follows:
source/
|──index.rst
|──module.rst
|──mymodule
|──index.rst
|──some_function.rst
|──subfolder
|──index.rst
|──another_function.rst
The module.rst which was automatically generated contains:
mymodule
====
.. toctree::
:maxdepth: 4
mymodule
So I call modules.rst in index.rst and run
.\make.bat html
and get the warning
docs\source\modules.rst:4: WARNING: toctree contains reference to nonexisting document 'mymodule'
I understood that this is somehow related to the fact that mymodule is a folder but toctree expects a .rst file.
I am a little bit confused why sphinx-nested-apidoc creates the mymodule folder and calls it into the toctree in module.rst.
What can I do so that the .rst files in the mymodule folder are recognized?
Related
I have a project including a Python package as well as some Matlab code, which should all go into the same documentation created by Sphinx.
The Python part of the documentation works flawlessly quite some time already - the Matlab part was added now and makes some trouble.
The data structure more or less is as follows:
|- python
|- modules
|- including subpackages
|- some sub-modules
|- matlab
|- scripts
|- functions
|- docs
|- source
|- conf.py
|- matlab.rst
|- python.rst
|- index.rst
|- ...
The most relevant lines in conf.py are prob. the following:
import os
import sys
# for the Python autodoc package
sys.path.insert(0, os.path.abspath('../..'))
extensions = ['sphinx.ext.napoleon', 'sphinxcontrib.matlab', 'sphinx.ext.autodoc']
# Path to the Matlab files
matlab_src_dir = '../../matlab'
#matlab_src_dir = '<<abssolute_path>>/matlab'
As you see, I tried both relative and absolute paths for matlab_src_dir. So far, without any difference.
The python.rst, which was created automatically by apidoc, contains:
python package
==============
.. automodule:: python
:members:
:undoc-members:
:show-inheritance:
Subpackages
-----------
.. toctree::
:maxdepth: 4
python.subpackages
The matlab.rst was created manually and contains:
matlab package
==============
.. mat:automodule:: matlab
:members:
:undoc-members:
:show-inheritance:
Subscripts
----------
.. toctree::
:maxdepth: 4
matlab.function_file
Thus, python.rst and matlab.rst are more or less the same but with mat:automodule for Matlab according to the documentation.
Finally, index.rst contains:
.. toctree::
:maxdepth: 2
python
.. toctree::
:maxdepth: 2
matlab
Now, when I run Sphinx' make html I receive the following error, which results in a nice Python documentation but an empty Matlab documentation:
WARNING: [sphinxcontrib-matlabdomain]: failed to import module 'matlab'; the following exception was raised:
Traceback (most recent call last):
File "C:\Users\<<username>>\AppData\Roaming\Python\Python39\site-packages\sphinxcontrib\mat_documenters.py", line 117, in import_object
obj = self.module = modules[self.modname]
KeyError: 'matlab'
What am I missing here. Why can't Sphinx find the matlab folder with the containing m-files?
And if that should be solved, maybe a secondary question: is there any similar function as apidoc for Matlab files, so that I do not need to create all *.rst files myself?
Thank you for any hints!
BTW: I am running on Sphinx v4.4.0 under Python 3.9.7.
Neither providing a hard-coded absolute path to matlab_src_dir nor providing os.path.abspath(relative_path) lead to success.
Thanks to Steve Piercy, who mentioned an example in the comments, I found that I should use os.path.dirname(os.path.abspath(relative_path)). As such, the errors are gone and Sphinx is working.
This is interesting, because I have already tried os.path.abspath(relative_path to the parent of the package) before, which I would expect is doing the same than os.path.dirname(). Anyway, I got a working solution now.
If anybody would have an idea about the second question ("is there any similar function as apidoc for Matlab files, so that I do not need to create all *.rst files myself?") I would be really happy.
I am autogenerating documentation for a Python package using Sphinx with the autodoc extension. The issue I am facing is that the autodoc skips any modules with an underscore.
Some modules were underscored to discourage users from importing them. However, the classes inside these underscored files have no underscores.
When I add the underscored module manually to the package_name.rst and ran make html, it shows. So my problem is how to automate that from autodoc.
I am trying to avoid parsing the package_name.rst via a script to add them. I am hoping for an autodoc flag or a hack!
It's perhaps a misconception to think the ..automodule :: directive applied to a package will automatically document sub-modules as members (by comparison with classes, variables, etc).
I just tested this but it can not be done using :private-members: and or :special-members:. Not by writing either option in the .. automodule:: directive corresponding to the package. (Trying to set both options in autodoc_default_options gives the same result.)
The following example of package and module layout:
C:.
│
└────your_package
│
│ public_module.py
│ _private_module.py
│ __init__.py
Using a .rst with a single .. automodule:: for the package:
Your package rst
================
.. automodule:: your_package
:members:
:undoc-members:
:private-members:
:special-members:
Minimal example _private_module.py with docstrings (public_module.py is the same except for the title):
"""Private module docstring."""
class PublicClass:
"""Docstring."""
pass
Does indeed give an empty documentation:
But if you remove the underscore from the module you get the exact same result.
I am trying to avoid parsing the package_name.rst via a script to add them
If you are generating the .rst files with sphinx-apidoc if using the -P flag:
-P, --private
Include “_private” modules.
New in version 1.2.
The generated files will include an .. automodule:: directive for the private modules, this does have the side-effect of also including the :private-members: option as noted in another post "Include __main__.py in sphinx-apidoc generated files".
Example explicitly including the .. automodule:: directives:
Your package rst
================
.. automodule:: your_package
:members:
:undoc-members:
.. automodule:: your_package.public_module
:members:
:undoc-members:
.. automodule:: your_package._private_module
:members:
:undoc-members:
The result:
As M2R is deprecated in Sphinx3. I tried to use recommonmark in order to include README.md to my documentation.
I have the following structure
project
|--README.md
|--docs
|--|--conf.py
|--|--index.rst
|--|--...
In conf.py I added recommonmark to extensions. and have '.md' in the source_suffix
In the index.rst
I have
.. toctree::
:maxdepth: 2
../README.md
modules
But Sphinx couldn't find it:
WARNING: toctree contains reference to nonexisting document 'README'
I can solve it by creating a softlink of README to the docs folder. But this doesn't seem quite right for version control. As I worries the build will break on other machines, or in future changes of configuration.
So is there a way to include README.md from the project directory to index.rst?
Many thanks
You can use an indirection.
index.rst
.. toctree::
readme
readme.rst
.. include:: ../README.md
Pyramid docs do this.
I am not able to add my __main__.py file correctly and its functions while generating RST file with sphinx-apidoc. Other files and classes are generated correctly.
I works only if I run sphinx-apidoc with -P parameter which includes private modules. But I do not want to add private methods of other modules, I need these from __main__.py only.
__main__.py looks like this:
def main():
"""
main() description here
"""
f1()
f2()
if __name__ == '__main__':
main()
I would like to have main(), f1() and f2() included in RST files generated by sphinx-apidoc.
There is a similar question Documenting python script entry (__name__ == '__main__') using sphinx but it does not answer my question.
I think this is an undocumented feature, or a it can also be bug in sphinx-apidoc v.3.2.1. If we look at the documentation for -P option, it reads:
-P, --private
Include “_private” modules.
Notice this doesn't mention :private-members: from autodoc flag options.
Two different issues are conflated, "private modules" and "private objects inside a module". The options that impact .rst file generation should be different for each according to the official documentation.
sphinx-apidoc will generate the .rst files based of 2 main templates, module.rst_t and package.rst_t. (Using Sphinx with a venv on Windows these are found under /venv/Lib/site-packages/sphinx/templates/apidoc).
The default behaviour (implemented by the templates) is to generate 1 .rst file per package, and within that file place 1 .. automodule:: directive per module. What the -P option does (supposedly) is to add one more directive .. automodule:: your-package.__private-module__ to the .rst file per private module.
An alternative is using -e option with sphinx-apidoc in which case a separate .rst file is generated for each module and package. So using sphinx-apidoc -e -P will cause an extra .rst file to be generated for private modules.
But I do not want to add private methods of other modules
Private classes/methods/variables (objects inside a module) are impacted by the autodoc ':private-members:' option.
sphinx-apidoc sets the default autodoc options of the generated .. automodule:: directives as defined by the SPHINX_APIDOC_OPTIONS environment variable (namely, :members:, :undoc-members: and show-inheritance). These options can't be passed as a command line argument, you have to set the environment variable before running sphinx-apidoc to change the default values. (sphinx-apidoc does not take them from conf.py, unlike autodoc.)
Looking at the source code of apidoc.py
# automodule options
if 'SPHINX_APIDOC_OPTIONS' in os.environ:
OPTIONS = os.environ['SPHINX_APIDOC_OPTIONS'].split(',')
else:
OPTIONS = [
'members',
'undoc-members',
# 'inherited-members', # disabled because there's a bug in sphinx
'show-inheritance',
]
Because :private-members: is a default autodoc options that should be set using SPHINX_APIDOC_OPTIONS (as documentation states and source code shows). If you include the -P option, its only (documented) effect should be adding .. automodule:: directives for private modules, what does happen is that it also sets the autodoc option :private-members: on each directive.
The following tree:
your_package
├ one_module.py
├ __init__.py
└ __main__.py
With sphinx-apidoc -P will generate:
your_package.__main__ module
----------------------------
.. automodule:: your_package.__main__ <<-- -P option is documented as having this effect.
:members:
:undoc-members:
:show-inheritance:
:private-members: <<-- -P option is not documented to have this effect.
So how to achieve the stated aim in the question?
If you use -e option with sphinx-apidoc generating one .rst file per module, you could use the [EXCLUDE_PATTERN …] from the signature. By running sphinx-apidoc twice, once for the __main__.py modules (together with -P option), and a second time for the remaining modules.
If you don't want individual .rst files for you modules, but instead the normal 1 .rst file for each package containing one directive per module. Then there is no practical possibility of achieving that (without resorting to considerable hacking). Your best choice is simply copy-pasting 1 .. automodule:: directive per __main__.py into the .rst files after they are generated.
I used the sphinx-quickstart to set everything up. I used doc/ for the documentation root location. The folder containing my package is setup as:
myfolder/
doc/
mypackage/
__init__.py
moprob.py
...
After the quick start, I edited the path in conf.py to be:
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
Then I added one of the scripts in my package to index.rst to see how Sphinx works.
.. toctree::
:maxdepth: 2
:caption: Contents:
mypackage/moprob
The error code I get:
.../index.rst:9: WARNING: toctree contains reference to nonexisting document u'mypackage/moprob'
Solutions I have tried:
Adding sphinx.ext.napoleon to the extensions list since all of my doc strings are written using the NumPy format. The error did not go away. I also put the napoleon extension after autodoc because one of the documentation pages suggested that.
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.napoleon']
Adding numpydoc_show_class_members = False to conf.py. I put this directly below the extensions but it also did not solve the error.
A couple of different configurations for folder locations. I've also tried setting the root location to be /myfolder and setting the source to be /mypackage and the build to be /doc. None has worked.
The toctree directive contains references to reStructuredText documents, not Python scripts or modules. Sphinx expects there to be a mypackage/moprob.rst file (in the doc folder), but there isn't one. Hence the error.
To quickly get some meaningful output, create the mypackage/moprob.rst file. Add a heading and an automodule directive in it:
moprob module
=============
.. automodule:: mypackage.moprob
:members:
Then run sphinx-build again.