[Distutils] using sub_commands in distutils

P.J. Eby pje at telecommunity.com
Fri May 14 17:38:30 CEST 2010

At 05:08 PM 5/14/2010 +0200, Manlio Perillo wrote:
>In a package, I have gettext catalog messages, and I want to compile
>them when the package is build.
>I looked at the Mercurial setup.py script, and what it does is:
>from distutils.command.build import build
>build.sub_commands.insert(0, ('build_mo', None))
>Is this the correct way?

No.  The correct way is to subclass the build command and use a 
cmdclass dictionary in the setup() call.  Something like:

from distutils.command.build import build
class build(build):
     sub_commands = [('build_mo', None)]+ build.sub_commands

     cmdclass = dict(build=build)

The way Mercurial is doing it is monkeypatching that will break if 
the current Python process runs more than one setup()...  e.g. when 
the setup script is being run by easy_install or some similar tool.

(As you can see, though, it only adds one line of code to do it the 
correct way, as documented in the distutils manual.)

More information about the Distutils-SIG mailing list