'Sphinx unable to import some modules

I am using to generate documentation for codebase. While building the docs, I could see that they were built for some modules only and many were left undocumented with warning in console - failed to import <module-name>.Even after going through official documentation and multiple answers on stackoverflow, I am unable to get why it's not working.

Following is structure of codebase:-

docs
├── Makefile
├── make.bat
├── source
│   ├── _templates
│   │   ├── custom-class-template.rst
│   │   └── custom-module-template.rst
│   ├── conf.py
│   └── index.rst
extract
│   ├── __init__.py
│   ├── avro_converter
│   │   ├── JSON_converter.py
│   │   ├── __init__.py
│   ├── helpers
│   │   ├── __init__.py
│   │   └── helpers.py

In index.rst file, I am trying to import documentation for extract module as follows [ref]:-

Welcome to documentation!
======================================

Modules
======================================

.. autosummary::
   :toctree: _autosummary
   :template: custom-module-template.rst
   :recursive:

   extract

Docs are rendered using following template:-

custom-module-template.rst

{{ fullname | escape | underline}}

.. automodule:: {{ fullname }}

   {% block attributes %}
   {% if attributes %}
   .. rubric:: Module attributes

   .. autosummary::
      :toctree:
      :nosignatures:
   {% for item in attributes %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

   {% block functions %}
   {% if functions %}
   .. rubric:: {{ _('Functions') }}

   .. autosummary::
      :toctree:
      :nosignatures:
   {% for item in functions %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

   {% block classes %}
   {% if classes %}
   .. rubric:: {{ _('Classes') }}

   .. autosummary::
      :toctree:
      :template: custom-class-template.rst
      :nosignatures:
   {% for item in classes %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

   {% block exceptions %}
   {% if exceptions %}
   .. rubric:: {{ _('Exceptions') }}

   .. autosummary::
      :toctree:
      :nosignatures:
   {% for item in exceptions %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

{% block modules %}
{% if modules %}
.. autosummary::
   :toctree:
   :template: custom-module-template.rst
   :nosignatures:
   :recursive:
{% for item in modules %}
   {{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

For building docs, following is configuration in config.py :-

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

project = 'sample'
copyright = '2021, Vineet Sharma'
author = 'Vineet Sharma'

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.autosummary',
    'sphinx.ext.coverage',
    'sphinx.ext.napoleon',
    'sphinx.ext.viewcode'
]

templates_path = ['_templates']
exclude_patterns = ['_build', '_templates']
html_theme = 'sphinx_rtd_theme'

html_static_path = []
autosummary_generate = True

For the file JSON_converter, there are few dependencies used by functions inside it as follows:-

JSON_converter.py

import json
import multiprocessing
import os
import random
import string
import sys
import traceback
from configparser import ConfigParser
from datetime import datetime
from imp import reload

import boto3
import fastavro
import pandas as pd

# get current directory
current_dir = os.getcwd()
sys.path.append(os.path.join(os.path.dirname(current_dir)))
reload(sys)

# helpers in another folder inside extract which contains a file named helpers.py; this file gets documented without any error
from helpers.helpers import enqueue, make_local_dir, remove_dir, read_csv

# parent directory
config_path = os.path.join(os.path.dirname(current_dir), "config.ini")
CONFIG = ConfigParser()
CONFIG.read(config_path)

....
....

On running make html command I get following error:-

WARNING: [autosummary] failed to import 'extract.avro_converter.JSON_converter': no module named extract.avro_converter.JSON_converter

/Users/espm2381/epi/titan/docs/source/_autosummary/extract.avro_converter.rst.rst:24: WARNING: autosummary: failed to import extract.avro_converter.JSON_converter

One of the reason for such errors is not having modules installed in your environment. But, I have all dependencies installed in my environment as follows:-

alabaster==0.7.12
awscli==1.19.16
Babel==2.9.1
beautifulsoup4==4.9.3
boto3==1.17.16
botocore==1.20.16
certifi==2021.5.30
charset-normalizer==2.0.3
click==8.0.1
colorama==0.4.3
docutils==0.15.2
et-xmlfile==1.0.1
fastavro==1.3.4
furo==2021.7.5b38
idna==3.2
imagesize==1.2.0
jdcal==1.4.1
Jinja2==2.11.3
jmespath==0.10.0
joblib==1.0.1
MarkupSafe==1.1.1
numpy==1.20.1
openpyxl==3.0.6
packaging==21.0
pandas==1.2.2
pyarrow==3.0.0
pyasn1==0.4.8
pydata-sphinx-theme==0.6.3
Pygments==2.9.0
pyparsing==2.4.7
python-dateutil==2.8.1
python-snappy==0.6.0
pytz==2021.1
PyYAML==5.4.1
requests==2.26.0
rsa==4.7.2
s3transfer==0.3.4
scikit-learn==0.24.1
scipy==1.7.0
sentry-sdk==1.1.0
six==1.15.0
snowballstemmer==2.1.0
soupsieve==2.2.1
Sphinx==4.1.2
sphinx-book-theme==0.1.1
sphinx-rtd-theme==0.5.2
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
threadpoolctl==2.2.0
urllib3==1.26.3

It has been a week since I have been trying to figure out the issue but all in vain. Would appreciate a suggestion or hint on the issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source