[Python-ideas] PEP for executing a module in a package containing relative imports
jimjjewett at gmail.com
Fri Apr 20 17:18:51 CEST 2007
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
(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
(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?
More information about the Python-ideas