I don’t like console-scripts. Among what I dislike is their magicality and the actual code produced. I mean, the autogenerated Python looks like:
#!/home/moshez/src/mainland/build/toxenv/bin/python2 # -*- coding: utf-8 -*- import re import sys from tox import cmdline if __name__ == '__main__': sys.argv = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv) sys.exit(cmdline())
Notice the encoding header for an ASCII only file, and the weird windows compatibility maybe generated along with a unix-style header that also contains the name of the path? Truly, a file that only its generator could love.
Then again, I don’t much like what we have in Twisted, with the preamble:
#!/home/moshez/src/ncolony/build/env/bin/python2 # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. import os, sys try: import _preamble except ImportError: sys.exc_clear() sys.path.insert(0, os.path.abspath(os.getcwd())) from twisted.scripts.twistd import run run()
This time, it’s not auto-generated, it is artisanal. What it lacks in codegen ugliness it makes up in importing an under-module at top-level, and who doesn’t like a little bit of sys.path games?
I will note that furthermore, neither of these options would play nice with something like pex. Worst of all, all of these options, regardless of their internal beauty (or horrible lack thereof), must be named veeeeery caaaaarefully to avoid collision with existing UNIX, Mac or Windows command. Think this is easy? My Ubuntu has two commands called “chromium-browser” and “chromium-bsu” because Google didn’t check what popular games there are on Ubuntu before naming their browser.
Enter the best thing about Python, “-m” which allows executing modules. What “-m” gains in awesomeness, it loses in the horribleness of the two-named-module, where a module is executed twice, once resulting in sys.modules[‘__main__’] and once in sys.modules[real_name], with hilarious consequences for class hierarchies, exceptions and other things relying on identity of defined things.
Luckily, packages will automatically execute “package.__main__” if “python -m package” is called. Ideally, nobody would try to import __main__, but this means packages can contain only one command. Enter ‘mainland‘, which defines a main which will, carefully and accurately, dispatch to the right module’s main without any code generation. It has not been released yet, but is available from GitHub. Pull requests and issues are happily accepted!
Edit: Released! 0.0.1 is up on PyPI and on GitHub