Skip to content Skip to sidebar Skip to footer

Check If Sphinx Doc Called The Script

I am currently trying to generate sphinx documentation for scripts which use the ArcGIS arcpy library. I am running into an issue when sphinx tries to run the scripts while genera

Solution 1:

The solution I came up with, while probably no-where near ideal, is to simply check

if'sphinx'in sys.modules:
    in_mxds = [r"C:/test.mxd"]
else:
    in_mxds = arcpy.GetParameterAsText(1)

This will ensure the script is not trying to get a parameter from the GUI which isn't set when generating sphinx documents.

Solution 2:

I do something like this. In my conf.py I add a new variable to the builtins module like:

# anywhere in conf.py before any of your modules are importedimport builtins
builtins.__sphinx_build__ = True

Then in my module code I can write a check like:

try:
    from some_dependency import SomeClass
except ImportError:
    try:
        if __sphinx_build__:
            classSomeClass:
                """Mock the class"""except NameError:
        raise ImportError('some_dependency')

Solution 3:

If your project is importing sphinx (in my case a sphinx extension), the following may also work for you

import os
import sys
if os.path.basename(sys.argv[0]) == "sphinx-build":
    # code for when sphinx is runningelse:
    # code for regular application

I'm not sure if that will work on Windows or if it should be something like

if os.path.basename(sys.argv[0]) in ["sphinx-build", "sphinx-build.exe"]:

Solution 4:

Since Sphinx 1.3 there is a simple solution for this issue. Just add

autodoc_mock_imports = ['arcpy']

to your conf.py. This can be used when some external dependencies are not met at build time and break the building process. See Sphinx: how to exclude imports in automodule?.

It's unfortunate that Esri has very poor arcpy documentation and mostly ignores Python standards.

Post a Comment for "Check If Sphinx Doc Called The Script"