Import config from sphinx subfolder
Question: I've created a sphinx project in a subfolder ("docs") inside my project root.
When I run the make file autodoc it cannot find the config file and raises
errors.

Is there a way to resolve this?

[project
structure](https://www.syshell.net/extra_upload/37608030/project_structure.txt)

[conf.py](https://www.syshell.net/extra_upload/37608030/conf.py)

[Makefile](https://www.syshell.net/extra_upload/37608030/Makefile)

[myreplicator.py](https://www.syshell.net/extra_upload/37608030/myreplicator.py)

[myreplicator.rst](https://www.syshell.net/extra_upload/37608030/myreplicator.rst)

This is the error:

    
    
    /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'
    

Thanks, Giovanni


Answer: I have to your folder structure diagram looks very messy.

Well, so from what I can see, there are two possible reasons.

### 1\. System PATH

You didn't put the directory of `myreplicator.py` into the `sys.path` so that
the `sphinx.autodoc.py` cannot locate your module (since it will only search
the module in system path. Therefore you need to insert your project folder
into the `sys.path` first). Which can be done by including the following in
`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\. Third-party Pacakge

Inside the `myreplicator.py`, there are some third-party modules cannot be
loaded, most likely some C type libs (like `.pyd`). In this case, you can mock
those third-party modules which cannot be imported by using below codes

    
    
    from mock import MagicMock, Mock  # if you are using python2
    # from unittest.mock import MagicMock, Mock # if you are using python3
    
    class Mock(MagicMock):
        @classmethod
        def __getattr__(cls, name):
                return Mock()
    
    MOCK_MODULES = ['mymongolib ', 'numpy', 'pandas'] # packages you want to mock
    sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)
    

However, from the exception you post, the chance of this error is small.

Overall, I think most likely the error is due to the first reason.

