1

I am using Python 3.9.2 with sphinx-build 3.5.2. I have created a project with the following directory and file structure

core_utilities
  |_core_utilities
  |   |_ __init__.py
  |   |_read_files.py
  |_docs
      |_ sphinx
           |_Makefile
           |_conf.pg
           |_source
           |    |_conf.py
           |    |_index.rst
           |    |_read_files.rst
           |_build

The read_files.py file contains the following code. NOTE: I simplified this example so it would not have distracting information. Within this code their is one class that contains one member function to read a text file, look for a key word and read the variable to the right of the keyword as a numpy.float32 variable. The stand-alone function written to read in a csv file with specific data types and save it to a pandas dataframe.

# Import packages here
import os
import sys
import numpy as np
import pandas as pd
from typing import List

class ReadTextFileKeywords:

    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'))
# ----------------------------------------------------------------------------

    def read_double(self, key_words: str) -> np.float64:
        values = self.read_sentence(key_words)
        values = values.split()
        return np.float64(values[0])

# ================================================================================
# ================================================================================


def read_csv_columns_by_headers(file_name: str, headers: List[str],
                                data_type: List[type],
   
    if not os.path.isfile(file_name):
        sys.exit('{}{}{}'.format('FATAL ERROR: ', file_name, ' does not exist'))
    dat = dict(zip(headers, data_type))
    df = pd.read_csv(file_name, usecols=headers, dtype=dat, skiprows=skip)
    return df

The conf.py file as the following information in it.

# 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 = 'my copyright'
author = 'my name'

# 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
autodock_mock_imports = ['numpy']

# 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']

The index.rst file has the following information.

. 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:

   read_files


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

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

and the read_files.rst file has the following information

**********
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:: test.ReadTextFileKeywords
   :members:

.. autofunction:: test.read_csv_columns_by_headers

In addition, I am using a virtual environment that is active when I try to compile this with command line. I run the following command to compile html files with sphinx.

sphinx-build -b html source build

When I run the above command it fails with the following error.

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

If I delete the line from pandas import pd and then delete the function read_csv_columns_by_headers along with the call to the function in the read_files.rst file, everything compiles fine. It appears that for some reason sphinx is able to find numpy, but it for some reason does not seem to recognize pandas, although both exist in the virtual environment and were loaded with a pip3 install statement. Does anyone know why sphinx is able to find other modules, but not pandas?

Jon
  • 1,621
  • 5
  • 23
  • 46
  • How is this different from https://stackoverflow.com/q/67101030/407651? – mzjn Apr 19 '21 at 15:57
  • Is there a `pandas` subdirectory in the virtualenv's `Lib/site-packages` directory? What happens if you start a Python session in the virtualenv and type `import pandas`? – mzjn Apr 19 '21 at 18:36

1 Answers1

0

Add pandas to autodoc

autodoc_mock_imports = ['numpy','pandas']
buddemat
  • 4,552
  • 14
  • 29
  • 49