Sphinx cannot find my python files. Says 'no module named …'

后端 未结 3 1739
太阳男子
太阳男子 2020-12-06 23:09

I have a question regarding the Sphinx autodoc generation. I feel that what I am trying to do should be very simple, but for some reason, it won\'t work.

I have a P

3条回答
  •  情书的邮戳
    2020-12-06 23:38

    Let's take an example with a project: dl4sci-school-2020 on master branch, commit: 6cbcc2c72d5dc74d2defa56bf63706fd628d9892:

    ├── dl4sci-school-2020
    │   ├── LICENSE
    │   ├── README.md
    │   ├── src
    │   │   └── __init__.py
    │   └── utility
    │       ├── __init__.py
    │       └── utils.py
    

    and utility package has a utils.py module:

    Follow this process(FYI, I'm using sphinx-build 3.1.2):

    1. create a docs/ directory under you project:
    mkdir docs
    cd docs
    
    1. start sphinx within docs/, and just pass your project_name, your_name & version of your choice and rest keep defaults.
    sphinx-quickstart
    

    you will get below auto-generated in your docs/ folder

    ├── docs
    │   ├── Makefile
    │   ├── build
    │   ├── make.bat
    │   └── source
    │       ├── _static
    │       ├── _templates
    │       ├── conf.py
    │       └── index.rst
    

    Since, we created a separate docs directory so we need sphinx find where to find build files and python src module. So, edit the conf.py file, you can use my conf.py file too

    import os
    import sys
    basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
    sys.path.insert(0, basedir)
    

    Now, to enable access to nested multiple packages & modules if any, you need to edit index.rst file.

    .. toctree::
       :maxdepth: 2
       :caption: Description of my CodeBase:
    
       modules
    

    The modules picks up content from modules.rst file which we will create below: Make sure you're still in doc/ to run the below command

    sphinx-apidoc -o ./source ..
    

    The output you get:

    ├── docs
    │   ├── Makefile
    │   ├── build
    │   ├── make.bat
    │   └── source
    │       ├── _static
    │       ├── _templates
    │       ├── conf.py
    │       ├── index.rst
    │       ├── modules.rst
    │       ├── src.rst
    │       └── utility.rst
    

    now run:

    make html
    

    Now, go and open in browser of your choice,

    file:////dl4sci-school-2020/docs/build/html/index.html

    have you beautiful documentation ready

    https://imgur.com/5t1uguh

    FYI, You can switch any theme of your choice, I found sphinx_rtd_theme and extension sphinxcontrib.napoleon super dope!. Thanks to their creators, so I used it.

    below does the work!

    pip install sphinxcontrib-napoleon
    pip install sphinx-rtd-theme
    

    You can host your documentation it on readthedocs enjoy documenting your code!

提交回复
热议问题