[Python-ideas] PEP for executing a module in a package containing relative imports

Steven Bethard steven.bethard at gmail.com
Mon Apr 23 03:18:51 CEST 2007


On 4/22/07, Aahz <aahz at pythoncraft.com> wrote:
> On Sun, Apr 22, 2007, Steven Bethard wrote:
> > On 4/22/07, Christian Heimes <lists at cheimes.de> wrote:
> >>
> >> I'm proposing the following changes:
> >>
> >> * sys.main is added which contains the dotted name of the main script.
> >>    This allows code like:
> >>
> >>      if __name__ == sys.main:
> >>          ...
> >
> > Note that this really requires the code::
> >
> >     import sys
> >     if __name__ == sys.main:
> >
> > The import statement matters to me because 77% of my modules that use
> > the __main__ idiom *don't* import sys. Hence, for those modules, this
> > new idiom introduces more boilerplate.
>
> Does this follow the axiom that 83% of all statistics are made up on the
> spot?  ;-)  Seriously, if I'm writing a script that requires __main__,
> chances are excellent that it already includes sys (because it's
> probably a command-line script that's graduating to module status).

No, I actually went and counted in my local repository. There are two
main reasons why that's true:
(1) Most unittest modules just run unittest.main(), so no import of sys.
(2) Most other modules use optparse or argparse, so no import of sys.

Steve
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy



More information about the Python-ideas mailing list