0

Problem:

Sphinx does not display documented code -only table of contents. I have isolated this down to Sphinx not generating the *.rst files for each documented *.py file when calling sphinx-apidoc.

Note:

I am using vscode with autodoc, the autodoc format is set to sphinx in the vscode settings file.

My project tree:

.
└── MNF
    ├── classes
    │   ├── file_parser.py
    │   └── README.txt
    ├── configs
    │   ├── datasets.json
    │   └── README.txt
    ├── data_in
    │   └── README.txt
    ├── data_out
    │   └── README.txt
    ├── LICENSE
    ├── models
    │   └── README.txt
    ├── notebooks
    │   ├── eda.ipynb
    │   └── README.txt
    ├── packages.microsoft.gpg
    ├── papers
    │   ├── conda-cheatsheet.pdf
    │   ├── dejuan2013.pdf
    │   ├── Git-Cheatsheet.pdf
    │   └── READE.txt
    ├── plots
    │   └── README.txt
    ├── __pycache__
    │   └── file_parser.cpython-39.pyc
    ├── README.md
    ├── src
    │   └── README.txt
    ├── test_branch.txt
    ├── test_main.txt
    ├── tests
    │   └── README.txt
    └── utilities
        └── README.txt

Code of interest:

class FileParser:
    """File parser
    """
    def __init__(self, root_path="../data_in/"):
        """
        :param root_path: root path to search isHiddenWithinTree, defaults to "../data_in/"
        :type root_path: str, optional
        """
        self.root_path = root_path

    def get_root_path(self):
        """Basic getter

        :return: root path
        :rtype: string
        """
        return self.root_path
    

Steps part 1:

  • mkdir docs
  • cd docs
  • sphinx-quickstart (seperatre source file, autodoc extention added)

modify conf.py:

import os
import sys
sys.path.insert(0, os.path.abspath('.')) 

to

import os
import sys
sys.path.insert(0, os.path.abspath('..'))

Change index.rst to include modules:

===============================

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

   modules



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

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

Steps part 2:

In docs dir

  • run sphinx-apidoc -o source/ ..
  • make html

New docs tree

.
├── docs
│   ├── build
│   │   ├── doctrees
│   │   │   ├── environment.pickle
│   │   │   ├── index.doctree
│   │   │   └── modules.doctree
│   │   └── html
│   │       ├── genindex.html
│   │       ├── index.html
│   │       ├── modules.html
│   │       ├── objects.inv
│   │       ├── search.html
│   │       ├── searchindex.js
│   │       ├── _sources
│   │       │   ├── index.rst.txt
│   │       │   └── modules.rst.txt
│   │       └── _static
│   │           ├── ajax-loader.gif
│   │           ├── alabaster.css
│   │           ├── basic.css
│   │           ├── comment-bright.png
│   │           ├── comment-close.png
│   │           ├── comment.png
│   │           ├── custom.css
│   │           ├── doctools.js
│   │           ├── down.png
│   │           ├── down-pressed.png
│   │           ├── file.png
│   │           ├── jquery-3.2.1.js
│   │           ├── jquery.js
│   │           ├── minus.png
│   │           ├── plus.png
│   │           ├── pygments.css
│   │           ├── searchtools.js
│   │           ├── underscore-1.3.1.js
│   │           ├── underscore.js
│   │           ├── up.png
│   │           ├── up-pressed.png
│   │           └── websupport.js

modules.rst file:

git_mnf
=======

.. toctree::
   :maxdepth: 4

Issue 1:

No file_parser.rst was generated.

Issue 2:

index.html page doesn't show any documentation

enter image description here

Help: I am at a lose with this one. Any ideas why my documentation for FileParse.py does not show?

Versions:

  • Python 3.9.2
  • sphinx 1.6.7
  • vscode 1.54.3
  • system: mac os
  • browser: firefox
mzjn
  • 48,958
  • 13
  • 128
  • 248
Dean
  • 141
  • 1
  • 7
  • If I understand this correctly the directories you are trying to document don't have `__init__.py` files and thus are namespace packages not regular Python packages. See [6.4 Packages](https://docs.python.org/3/tutorial/modules.html#packages). But there are a number of issues here, for example: using the usual layout all your `.py` files should be inside `/src/name_your_base_package` and in `conf.py` you'd use `os.path.join('..', '..', 'src')` to have the import from the package (with `__init__.py`) `name_your_base_package`. – bad_coder Mar 21 '21 at 07:24
  • This can be confusing, my suggestion would be to create a simple package/module pair (with `__init__.py`) and understanding how insertion of modules on [`sys.path`](https://docs.python.org/3/using/cmdline.html#cmdoption-m) works when you are executing something (because insertion and lookup also depends on how you execute things.) Here's a [good thread on module lookup](https://stackoverflow.com/q/4383571). Regarding this specific question, it's not entirely clear how you want to structure your project so it isn't really possible to give a meaningful answer. – bad_coder Mar 21 '21 at 07:31
  • 3
    Here are 2 examples [using `src` layout](https://stackoverflow.com/a/60159862) and [using normal layout](https://stackoverflow.com/a/59951675) that show how to run Sphinx in two different project layouts. From your shown structure it's hard to say if you've committed to one way or the other, but I would recommend considering [this example](https://stackoverflow.com/a/14421176). – bad_coder Mar 21 '21 at 07:40
  • Thank you. For me the solution was to change to src format structure and then modify the conf.py filepath to sys.path.insert(0, os.path.abspath('../../..')). I then changed the source and output dir for the sphinx-api command from: sphinx-api -o ./source .. To: sphinx-api -o ./source ../../ Everything works now :) – Dean Mar 21 '21 at 13:28
  • If you went for the `/src` layout you'll probably also want to do a [development install](https://python-packaging-user-guide.readthedocs.io/tutorials/installing-packages/#installing-from-a-local-src-tree). Here's a [simple example](https://stackoverflow.com/q/62498127). Choosing a layout correlates with how you configure you virtual environments and there are several possible solutions (from here on it would depend on your particular setup). But there's really no going wrong with the `/src` layout, it's what everyone uses. – bad_coder Mar 21 '21 at 14:17

0 Answers0