41

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 looks like I need to inherit from distutils.command.install, override some methods and add this object to the setup script. The specifics are a bit hazy though and it seems like a lot of effort for such a simple hook. Does anyone know an easy way to do this?

4 Answers 4

39

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), ...)
3
  • 2
    Thanks, Joe. I already found out and posted a similar answer. You were earlier though, so enjoy the green :)
    – blokkie
    Commented Aug 24, 2009 at 9:38
  • 1
    An example can be found in the setup.py of pycparser.
    – 0 _
    Commented Mar 27, 2015 at 8:18
  • Anyone know a good way to do this as a library author? I.e. so that everyone using the library (setup_requires) gets the new install command?
    – DylanYoung
    Commented May 23, 2020 at 3:50
19

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.

1
  • 1
    been looking around on how to add custom commands for ages and nothing that I found would work, till I found this post of yours! thanks!
    – dm76
    Commented Jan 18, 2013 at 12:08
8

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.

2
  • 2
    Joe Wreschnig's answer didn't work because distutils.command.install is the install module, the class he intended to extend was distutils.command.install.install.
    – ohmu
    Commented Dec 28, 2012 at 17:28
  • 3
    @cpburnz I fixed the other answer since it's most likely what people will try first.
    – kynan
    Commented Jul 23, 2013 at 11:21
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.