0

I am trying to document a Python class using sphinx. I am used Python 3.9.2 and sphinx-build 3.5.2. I am running the command sphinx-build -b html source build to transform the .rst files into .html files. I have already successfully executed this configuration on other classes and functions in the code-suite, so I know their is nothing wrong with the way I have organized my files. When I run the command I get the following error;

Warning: autodoc: failed to import class 'ReadTextFileKeywords' from module 'read_files'; the following exception was raised: No module named pandas

I do have the most recent version of pandas loaded globally and in my virtual environment. Does anyone know why it cannot load pandas and how I can fix this issue?

For reference, the file structure looks like this;

core_utilities
  |_core_utilities
  |_docs
      |_sphinx
          |_Makefile
          |_build
          |   |_html files
          |_source
              |_index.rst
              |_Introduction.rst
              |_read_files.rst
              |_operating_system.rst
              |_conf.py

The conf.py file has the following information

# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('../../../core_utilities'))

# -- Project information -----------------------------------------------------

project = 'Core Utilities'
copyright = '2021, Jonathan A. Webb'
author = 'Jonathan A. Webb'

# The full version, including alpha/beta/rc tags
release = '0.1.0'


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.todo', 'sphinx.ext.viewcode', 'sphinx.ext.autodoc',
              'sphinx.ext.autosummary', 'sphinx.ext.githubpages']
autodoc_member_order = 'groupwise'
autodoc_default_flags = ['members', 'show-inheritance']
autosummary_generate = True

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'nature'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

and the index file has the following format

.. Core Utilities documentation master file, created by
   sphinx-quickstart 
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to Core Utilities's documentation!
==========================================

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

   Introduction
   operating_system
   read_files


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

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

Sphinx works just fine when I import docstrings from the operating_system module, so I know that is not the problem. It fails when I try to import from the read_files module. The read_files.rst file has hte following format.

**********
read_files
**********

The ``read_files`` module contains methods and classes that allow a user to read different
types of files in different ways.  Within the module a user will find functionality that 
allows them to read text files and csv files by columns and keywords.  This module
also contains functions to read sqlite databases, xml files, json files, yaml files, 
and html files

.. autoclass:: read_files.ReadTextFileKeywords
   :members:

And the class it is reading, to include the file has the following format

# Import packages here
import os
import sys
import numpy as np
import pandas as pd
from typing import List
import sqlite3
# ================================================================================
# ================================================================================
# Date:    Month Day, Year
# Purpose: Describe the purpose of functions of this file

# Source Code Metadata
__author__ = "Jonathan A. Webb"
__copyright__ = "Copyright 2021, Jon Webb Inc."
__version__ = "1.0"
# ================================================================================
# ================================================================================
# Insert Code here


class ReadTextFileKeywords:
    """
    A class to find keywords in a text file and the the variable(s)
    to the right of the key word.  This class must inherit the
    ``FileUtilities`` class


    :param file_name: The name of the file being read to include the
                      path-link

    For the purposes of demonstrating the use of this class, assume
    a text file titled ``test_file.txt`` with the following contents.


    .. code-block:: text

        sentence: This is a short sentence!
        float: 3.1415 # this is a float comment
        double: 3.141596235941 # this is a double comment
        String: test # this is a string comment
        Integer Value: 3 # This is an integer comment
        float list: 1.2 3.4 4.5 5.6 6.7
        double list: 1.12321 344.3454453 21.434553
        integer list: 1 2 3 4 5 6 7
    """
    def __init__(self, file_name: str):
        self.file_name = file_name
        if not os.path.isfile(file_name):
            sys.exit('{}{}{}'.format('FATAL ERROR: ', file_name, ' does not exist'))
Jon
  • 1,621
  • 5
  • 23
  • 46
  • autodoc imports your classes, meaning at the top of module `read_files` you should have `from pandas import whatever` because it's used in class `ReadTextFileKeywords`. When you say "I have (...) pandas loaded globally" do you use it in modules without writing imports at the top of the module? Strong probability is that you are executing Sphinx on the command line without having activated the venv (contrary to when you run the application) and that's why Sphinx can't find pandas in the venv. (Try activating the venv and running Sphinx again, it should work.) – bad_coder Apr 15 '21 at 02:04
  • venv is activated when I run the command from the CLI and it is not finding it. – Jon Apr 15 '21 at 03:43
  • You question doesn't have a [*"Minimal, Reproducible, Example."*](https://stackoverflow.com/help/minimal-reproducible-example), we can't see the class, the imports, the `.rst`or the configuration file. Does where `where pandas` find the library on the CMD where you run Sphinx? If it does, the problem lies in the classe/module/autodoc mentioned in the error message. – bad_coder Apr 15 '21 at 03:51
  • I just updated the question to contain more relevant detail – Jon Apr 15 '21 at 13:48
  • 1
    I don't know what your privacy concerns are, but I would strongly recommend editing out your name and raising a costum mod flag for the edit history to be sanitized. – bad_coder Apr 15 '21 at 13:53
  • 2 question, is there a `__init__.py` inside `core_utilities`? (there are 2 `core_utilities` please clarify which of the 2 are packages), Also, the file/dir tree does not show `read_files.ReadTextFileKeywords` where the module class is placed inside the package (that would be important). – bad_coder Apr 15 '21 at 13:58
  • Yes, there is an __init__.py file in the lower core_utilities package with is the package. – Jon Apr 15 '21 at 16:00
  • If you look at your `conf.py` the `sys.path.insert` is pointing inside the higher level core_utilities not the lower one (that's likely a mistake) but you really should edit the question as I explained earlier, your last comment doesn't really clarify enough where the files in question are in the tree. – bad_coder Apr 15 '21 at 16:12
  • I took a few days off and am just seeing your comment. I am happy to post more information if necessary. However, the top of my post shows the file structure, and I am showing the contents of those files. How does the information I have posted not show where the files are? Also, the conf.py does not point to the upper core_utilities directory, it is pointing to the lower one. – Jon Apr 19 '21 at 15:07
  • the `__init__.py` files are needed to know what is a regular package and what isn't. – bad_coder Apr 19 '21 at 17:40

0 Answers0