如何使BuildDoc仅在从安装pythonsphinx时工作设置.py文件?

2021-05-13 14:11:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个项目的setup.py文件FlashText

from setuptools import setup, Command
from sphinx.setup_command import BuildDoc

setup(
    .
    .
    cmdclass={'test': PyTest, 'build_sphinx': BuildDoc},
)

如果未安装python sphinx,pip install flashtext将失败。在

ImportError: No module named sphinx.setup_command


Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-LI0I_O/flashtext/

这将解决该问题:

^{pr2}$

我需要的是如果有人没有安装python-sphinx,那么他们也应该能够安装这个库。我该怎么处理?在

例如,py.test的处理方式如下:

import subprocess


class PyTest(Command):
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        errno = subprocess.call(['py.test'])
        raise SystemExit(errno)

PS:github上有完整的代码https://github.com/vi3k6i5/flashtext

1条回答
网友
1楼 ·
cmdclass={'test': PyTest}

try:
    from sphinx.setup_command import BuildDoc
    cmdclass['build_sphinx'] = BuildDoc
except ImportError:
    print('WARNING: sphinx not available, not building docs')

setup(
    .
    .
    cmdclass=cmdclass
)

相关问题