如何排除自动模块中的导入

如何排除自动模块中的导入

本文介绍了Sphinx:如何排除自动模块中的导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Python编写的Raspberry Pi项目,该项目使用RPi.GPIO模块。代码上的所有工作都在Windows盒子上完成,该窗口上不会安装RPi.GPIO,并且每次我尝试运行autodoc时都会崩溃,提示无法导入RPi.GPIO。

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 try to run autodoc it crashes saying it cannot import RPi.GPIO.

D:\cube\docs\ledcube.rst:4: WARNING: autodoc: failed to import module u'ledcube'
; the following exception was raised:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\sphinx-1.2b1-py2.7.egg\sphinx\ext\autodoc.
py", line 326, in import_object
    __import__(self.modname)
  File "D:\cube\ledcube.py", line 2, in <module>
    import RPi.GPIO as GPIO
ImportError: No module named RPi.GPIO

可以解决这个问题吗?

推荐答案

没有办法告诉Sphinx排除某些导入。使用自动文档时,所有记录在案的模块都必须是可干净导入的。

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

您也许可以通过执行一些。这是一篇描述您的问题的解决方案的文章:。这是一个小代码示例(打算添加到conf.py中):

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()

您可能需要安装python-mock才能使上述工作正常: sudo apt-get install python-模拟

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

自Sphinx 1.3起,设置起来更加容易嘲笑。只需将要模拟的模块添加到配置值。

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.

这篇关于Sphinx:如何排除自动模块中的导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:29