从sphinx子文件夹导入配置

2024-06-02 05:12:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在项目根目录下的子文件夹(“docs”)中创建了一个sphinx项目。当我运行make文件autodoc时,它找不到配置文件并引发错误。在

有没有办法解决这个问题?在

project structure

conf.py

Makefile

myreplicator.py

myreplicator.rst

这是错误:

/vagrant/python/mymongo/doc/myreplicator.rst:4: WARNING: autodoc: failed to import module 'myreplicator'; the following exception was raised:
Traceback (most recent call last):
  File "/var/venv/mymongo/lib/python3.5/site-packages/sphinx    /ext/autodoc.py", line 518, in import_object
    __import__(self.modname)
  File "/vagrant/python/mymongo/myreplicator.py", line 20, in <module>
logging.config.fileConfig('conf/logging.conf')
  File "/opt/py35/lib/python3.5/logging/config.py", line 76, in fileConfig
    formatters = _create_formatters(cp)
  File "/opt/py35/lib/python3.5/logging/config.py", line 109, in _create_formatters
    flist = cp["formatters"]["keys"]
  File "/opt/py35/lib/python3.5/configparser.py", line 956, in __getitem__
    raise KeyError(key)
KeyError: 'formatters'

谢谢, 乔瓦尼


Tags: inpyimportconfiglibloggingconfline
1条回答
网友
1楼 · 发布于 2024-06-02 05:12:54

我把你的文件夹结构图看得很乱。在

从我看来,有两个可能的原因。在

1。系统路径

您没有将myreplicator.py目录放入sys.path中,因此sphinx.autodoc.py无法找到您的模块(因为它只在系统路径中搜索模块。因此,您需要首先将项目文件夹插入sys.path。这可以通过在conf.py中包含以下内容来完成

import os
import sys
sys.path.insert(0, os.path.abspath('../')) # based on what I see in your structure, the myrepicator.py is in the parent directory of conf.py

2。第三方包装

myreplicator.py中,有一些第三方模块无法加载,很可能是一些C类型的lib(比如.pyd)。在这种情况下,您可以模拟那些第三方模块,这些模块不能通过下面的代码导入

^{pr2}$

但是,从您发布的异常来看,发生此错误的可能性很小。在

总的来说,我认为最有可能的错误是由于第一个原因。在

相关问题 更多 >