简体   繁体   中英

how to make BuildDoc work only when python-sphinx is installed from setup.py file?

I have a setup.py file for my project FlashText :

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

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

pip install flashtext fails if python-sphinx is not installed.

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/

This will fix that problem:

sudo apt-get install python-sphinx

What I need is if someone doesn't have python-sphinx installed then also they should be able to install the library. How should I handle that?

for example py.test is handled like this:

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: Complete code is available on github https://github.com/vi3k6i5/flashtext

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM