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

Steven Bethard steven.bethard at gmail.com
Sat Apr 21 00:14:44 CEST 2007

On 4/20/07, Christian Heimes <lists at cheimes.de> wrote:
> What about
>     import sys
>     if __name__ == sys.main:
>         ...
> You won't have to introduce a new global module var __name__ and it's
> easy to understand for newbies and experienced developers. The code is
> only executed when the name of the current module is equal to the
> executed main module (sys.main).

But you have to understand a few things to understand why this works.
You have to know that __name__ is the name of the module, and that if
you want to find out the name of the main module, you need to look at
sys.main.  With the idiom::

    if __main__:

all you need to know is that the main module has __main__ set to true.

> IMO it's much less PIT...B then introducing __main__.

Could you elaborate? Do you think it would be hard to introduce
another module-level attribute (like we already do for __name__)? Or
do you think that the code would be hard to maintain? Or something

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