How to extend distutils with a simple post install script?

后端 未结 4 2238
我寻月下人不归
我寻月下人不归 2020-12-01 03:09

I need to run a simple script after the modules and programs have been installed. I\'m having a little trouble finding straight-forward documentation on how to do this. It l

相关标签:
4条回答
  • 2020-12-01 03:13

    I dug through distutils source for a day to learn enough about it to make a bunch of custom commands. It's not pretty, but it does work.

    import distutils.core
    from distutils.command.install import install
    ...
    class my_install(install):
        def run(self):
            install.run(self)
            # Custom stuff here
            # distutils.command.install actually has some nice helper methods
            # and interfaces. I strongly suggest reading the docstrings.
    ...
    distutils.core.setup(..., cmdclass=dict(install=my_install), ...)
    
    0 讨论(0)
  • 2020-12-01 03:15

    I couldn't make Joe Wreschnigs answer work and tweaked his answer analogous to the extending distutils documentation. I came up with this code which works fine on my machine.

    from distutils import core
    from distutils.command.install import install
    ...
    class my_install(install):
        def run(self):
            install.run(self)
            # Custom stuff here
            # distutils.command.install actually has some nice helper methods
            # and interfaces. I strongly suggest reading the docstrings.
    ...
    distutils.core.setup(..., cmdclass={'install': my_install})
    

    Note: I didn't edit the answer of Joe since I am uncertain why his answer wasn't working on my machine.

    0 讨论(0)
  • 2020-12-01 03:18

    OK, I figured it out. The idea is basically to extend one of the distutils commands and overwrite the run method. To tell distutils to use the new class you can use the cmdclass variable.

    from distutils.core import setup
    from distutils.command.install_data import install_data
    
    class post_install(install_data):
        def run(self):
            # Call parent 
            install_data.run(self)
            # Execute commands
            print "Running"
    
    setup(name="example",
          cmdclass={"install_data": post_install},
          ...
          )
    

    Hope this will help someone else.

    0 讨论(0)
  • 2020-12-01 03:35

    I got an error when I tried the accepted answer here (might be because I'm using Python 2.6 in this particular case, not sure). This occurred for both 'setup.py install' and 'pip install':

    sudo python setup.py install
    

    fails with error: error in setup.cfg: command 'my_install' has no such option 'single_version_externally_managed'

    AND

    sudo pip install . -U
    

    fails more verbosely but also with error: option --single-version-externally-managed not recognized

    Variation on the accepted answer

    Replacing the imports from distutils with setuptools solved the problem for me:

    from setuptools import setup
    from setuptools.command.install import install
    
    0 讨论(0)
提交回复
热议问题