开发者

Python setuptools custom configuration

I'm packaging up a Python module, and I would like users to be able to build the module with some custom options. Specific开发者_Python百科ally, the package will do some extra magic if you provide it with certain executables that it can use.

Ideally, users would run setup.py install or setup.py install --magic-doer=/path/to/executable. If they used the second option, I’d set a variable somewhere in the code, and go from there.

Is this possible with Python's setuptools? If so, how do I do it?


It seems you can... read this.

Extract from article:

Commands are simple class that derives from setuptools.Command, and define some minimum elements, which are:

description: describe the command
user_options: a list of options
initialize_options(): called at startup
finalize_options(): called at the end
run(): called to run the command

The setuptools doc is still empty about subclassing Command, but a minimal class will look like this:

 class MyCommand(Command):
     """setuptools Command"""
     description = "run my command"
     user_options = tuple()
     def initialize_options(self):
         """init options"""
         pass

     def finalize_options(self):
         """finalize options"""
         pass

     def run(self):
         """runner"""
         XXX DO THE JOB HERE

The class can then be hook as a command, using an entry point in its setup.py file:

 setup(
     # ...
     entry_points = {
     "distutils.commands": [
     "my_command = mypackage.some_module:MyCommand"]}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜