When using autodoc, Sphinx imports documented modules, so all module-level code is executed. This happens every time you do "html". In this sense, Sphinx runs your code.
You may need to sort your code a bit so that the errors disappear (translate the module code into functions). See this question for an example of what might happen.
This is my guess, but it may not be the whole story. It is hard to say more without additional information.
source share