python - 如何将 Sphinx doctests 作为 setup.py 的一部分运行?

标签 python python-sphinx setuptools setup.py doctest

我想包含一个 setuptools 命令来运行 Sphinx doctest 作为我的包 setup.py 的一部分,如下所示:

$ python setup.py sphinx_doctest

我的包结构如下:

my_pkg
|---__init__.py
|---module1.py    # Containing reStructuredText docstrings with examples
docs
|---build
|---|---doctrees
|---source
|---|---conf.py   # Sphinx config
|---|---index.rst # Sphinx index file
setup.py

我如何实现一个 setuptools 命令,它的作用相当于:

$ sphinx-build -b doctest -d docs/build/doctrees docs/source docs/build

最佳答案

子类 setuptools.Command 并使用 sphinx.application.Sphinx启动 sphinx.ext.doctest build 者。

在 setup.py 中:

from setuptools import setup, Command

class Doctest(Command):
    description = 'Run doctests with Sphinx'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        from sphinx.application import Sphinx
        sph = Sphinx('./docs/source', # source directory
                     './docs/source, # directory containing conf.py
                     './docs/build', # output directory
                     './docs/build/doctrees', # doctree directory
                     'doctest') # finally, specify the doctest builder
        sph.build()

sphinx_requires = ['sphinx>=1.3.1']

setup(
    name='mypkg',
    version='0.0.1',
    description='My Package',
    packages=['mypkg'],
    cmdclass={
        'doctests': Doctest
    },
    extras_require={
        'build_sphinx': sphinx_requires,
    },
)

关于python - 如何将 Sphinx doctests 作为 setup.py 的一部分运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613013/

相关文章:

python - 为什么我的 GUI 没有响应,即使我外包给工作线程?

github - 构建 Sphinx 文档时 PandocMissing

python-sphinx - 狮身人面像 : include xlsx data into rst

python - 如果我有 setup.py,还需要 setup.cfg 吗?

python - easy_install 得到错误的 pip 版本

python - DeprecationWarning : firefox_binary has been deprecated, 请使用 Selenium Python 中的参数 firefox_binary 传入 Service 对象

python - PyCharm 无法解析多处​​理模块的动态引用

python - 如何在 flask-admin 中引用 ModelView

rest - 带有Sphinx的RESTful Web服务API文档

python setup.py develop 不更新 easy_install.pth