Sphinx: how to exclude imports in automodule?

前端 未结 3 1387
清歌不尽
清歌不尽 2020-12-03 03:02

I have a Raspberry Pi project written in Python that uses RPi.GPIO module. All the work on the code is done on a Windows box where RPi.GPIO will not install and every time I

3条回答
  •  伪装坚强ぢ
    2020-12-03 03:32

    There is no way to tell Sphinx to exclude some imports. When using autodoc, all documented modules must be cleanly importable.

    You might be able to work around the problem by doing some mocking. Here is an article describing the solution to a problem that seems quite similar to yours: http://blog.rtwilson.com/how-to-make-your-sphinx-documentation-compile-with-readthedocs-when-youre-using-numpy-and-scipy/. Here is a small code sample (intended to be added to conf.py):

    import mock
    
    MOCK_MODULES = ['numpy', 'matplotlib', 'matplotlib.pyplot']
    for mod_name in MOCK_MODULES:
        sys.modules[mod_name] = mock.Mock()
    

    You might might need to install python-mock for the above to work: sudo apt-get install python-mock

    Update

    Since Sphinx 1.3, it is easier to set up the mocking. Just add the modules to be mocked to the autodoc_mock_imports configuration value.

提交回复
热议问题