0

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.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Vineet
  • 723
  • 4
  • 12
  • 31
  • Try changing `sys.path.insert(0, os.path.abspath('../..'))` to `sys.path.insert(0, os.path.abspath('..'))`. Does that resolve the import error? – Steve Piercy Aug 02 '21 at 07:39
  • I changed `sys.path` as - `sys.path.insert(0, os.path.abspath('..')`. But it threw - `WARNING: [autosummary] failed to import 'extract': no module named extract; /Users/espm2381/epi/titan/docs/source/index.rst.rst:8: WARNING: autosummary: failed to import extract` – Vineet Aug 02 '21 at 09:06
  • Please let me know if I should provide any other details than mentioned in the question – Vineet Aug 02 '21 at 16:22
  • What is in `custom-module-template.rst`? Edit your question, do not paste it into a comment. – Steve Piercy Aug 03 '21 at 11:06
  • Added `custom-module-template.rst` – Vineet Aug 03 '21 at 15:16
  • reST cannot have Jinja2 syntax. You must put your templates in a template file. See https://www.sphinx-doc.org/en/master/development/theming.html – Steve Piercy Aug 03 '21 at 22:48
  • On GitHub, you have a similar sample Sphinx project. In that project, there are several "comments" like this one, `<-- add this line`, in the template files (https://github.com/Vineet-Sharma29/sample-sphinx/blob/master/docs/source/_templates). That is not proper comment syntax. I think you should remove these comments. – mzjn Aug 04 '21 at 15:06
  • @mzjn I had removed the comments; it didn't work even after that – Vineet Aug 04 '21 at 18:56
  • All I can say is this: I cloned your GitHub project and when I removed those comments, the errors disappeared. – mzjn Aug 04 '21 at 19:00
  • Yep, code in that repo works since it contains dummy files in it. However, when I run it on main codebase with dependencies as specified in the code in question it throws me an error. – Vineet Aug 05 '21 at 03:58
  • I think you need to provide a [mcve] based on your "main codebase". – mzjn Aug 07 '21 at 14:34

0 Answers0