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

Brett Cannon brett at python.org
Fri Apr 20 19:16:48 CEST 2007


On 4/20/07, Jim Jewett <jimjjewett at gmail.com> wrote:
> On 4/19/07, Brett Cannon <brett at python.org> wrote:
>
> > ...  By leaving the ``__name__`` attribute in a module alone and
> > setting a module attribute named ``__main__`` to a true value for the
> > main module (and thus false in all others) ...
>
> Part of me says that you are already proposing the right answer, as
> these alternatives are just a little too hackish.  Still, they are
> good enough that they should be listed in the PEP, even if only as
> rejected alternatives.
>
> (1)  You could add a builtin __main__ that is false.  The real main
> module would mask it, but no other code would need to change.
>
> Con:  Another builtin, and this one wouldn't even make sense as an
> independent object.
>
> (2)  You could special-case the import to use __file__ instead of
> __name__ when __name__ == "__main__"
>
> Con:  may be more fragile.
>
> (3)  You could set __name__ to (an instance of) a funky string
> subclass that overrides __eq__.
>
> Con:  may be hard to find exactly the *right* behavior.  Examples:
> What should str(name) do?  Maybe __main__ should be the primary value,
> and split should be overridden?
>

Yeah, I don't like any of them.  =)  I will add them to the PEP in a
Rejected Ideas section.

-Brett



More information about the Python-ideas mailing list