0

I'm working with sphinx to document a python project, but its not integrating well with python-dotenv. When running .\make, I'll get a warning that boils down to:

logging.getLogger(config['APP_NAME'])

KeyError: 'APP_NAME'

This led me to see if my app configuration (which uses dotenv) is working properly. I'm using the following structure to persist the configuration throughout the project:

from dotenv import dotenv_values
config = {
    **dotenv_values('.env.example'),
    **dotenv_values('.env'),
}

for key, val in config.items():
    try:
        if val in ['True', 'False']:
            config[key] = bool(val == 'True')
        else:
            config[key] = float(val)
    except ValueError:
        continue

I tried printing out the dotenv values, and am getting empty dictionaries. This leads me to believe that sphinx is somehow messing with imports?

The relevant file structure is as follows:

.
└── Project/
    ├── docs/
    │   ├── _build/
    │   │   └── html
    │   └── make.bat
    │   └── conf.py
    ├── src/
    │   └── main/
    │       ├── other files/directories
    │       └── app_config.py
    ├── .env
    └── .env.example

I normally wouldn't be too concerned about a warning, but any files that use the configuration won't be properly documented. The sphinx docs don't discuss any weird specifics required for using environment variables, and I can't find anything else with a similar problem.
In case its relevant, here's my conf.py file:

# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import os
import sys
sys.path.insert(0, os.path.abspath('..'))


# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

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

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'furo'
html_static_path = ['_static']

When printing os.getcwd() it'll say it's in the /docs directory (after running .\make html). I tried fixing this by using os.chdir('..') to put me in the root directory, but it did nothing.

I've tried running docs\make.bat html to see if it was the location from which make was being run that was affecting it, but still no change.

An obvious solution here is to just make a try/except block to manage when sphinx is building the project, but I want to understand why this is happening instead of using a hack solution.

The app runs normally, so is there some reason Sphinx would be causing this problem?

TheDeafOne
  • 93
  • 10

0 Answers0