Skip to content Skip to sidebar Skip to footer

How To Depends Of A System Command With Python/distutils?

I'm looking for the most elegant way to notify users of my library that they need a specific unix command to ensure that it will works... When is the bet time for my lib to raise a

Solution 1:

IMO, the best way is to check at install if the user has this specific *nix command.

If you're using distutils to distribute your package, in order to install it you have to do:

python setup.py build python setup.py install

or simply

python setup.py install (in that case python setup.py build is implicit)

To check if the *nix command is installed, you can subclass the build method in your setup.py like this :

from distutils.core import setup
from distutils.command.build import build as _build

classbuild(_build):

    description = "Custom Build Process"
    user_options= _build.user_options[:]
    # You can also define extra options like this : #user_options.extend([('opt=', None, 'Name of optionnal option')])definitialize_options(self):   

        # Initialize here you're extra options... Not needed in your case#self.opt = None
        _build.initialize_options(self)

    deffinalize_options(self):
        # Finalize your options, you can modify valueif self.opt isNone :
            self.opt = "default value"

        _build.finalize_options(self)

    defrun(self):
        # Extra Check# Enter your code here to verify if the *nix command is present
        .................

        # Start "classic" Build command
        _build.run(self)

setup(
        ....
        # Don't forget to register your custom build command
        cmdclass         = {'build' : build},
        ....
     )

But what if the user uninstall the required command after the package installation? To solve this problem, the only "good" solution is to use a packaging systems such as deb or rpm and put a dependency between the command and your package.

Hope this helps

Solution 2:

I wouldn't have any check at all. Document that your library requires this command, and if the user tries to use whatever part of your library needs it, an exception will be raised by whatever runs the command. It should still be possible to import your library and use it, even if only a subset of functionality is offered.

(PS: commands is old and broken and shouldn't be used in new code. subprocess is the hot new stuff.)

Post a Comment for "How To Depends Of A System Command With Python/distutils?"