Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

importerror - Sphinx: how to exclude imports in automodule?

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:cubedocsledcube.rst:4: WARNING: autodoc: failed to import module u'ledcube'
; the following exception was raised:
Traceback (most recent call last):
  File "C:Python27libsite-packagessphinx-1.2b1-py2.7.eggsphinxextautodoc.
py", line 326, in import_object
    __import__(self.modname)
  File "D:cubeledcube.py", line 2, in <module>
    import RPi.GPIO as GPIO
ImportError: No module named RPi.GPIO

Any way around this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...