[Python-3000] PEP to change how the main module is delineated

Brett Cannon brett at python.org
Mon Apr 23 22:00:18 CEST 2007


On 4/23/07, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Brett Cannon wrote:
> > This PEP is to change the ``if __name__ == "__main__": ...`` idiom to
> > ``if __name__ == sys.main: ...``  so that you at least have a chance
> > to execute module in a package that use relative imports.
> >
> > Ran this PEP past python-ideas.  Stopped the discussion there when too
> > many new ideas were being proposed.  =)  I have listed all of them in
> > the Rejected Ideas section, although if overwhelming support for one
> > comes forward the PEP can shift to one of them.
>
> -1
>
> I still prefer the "__module_name__" fallback that was actually briefly
> in the tree for 2.5 (the change was made really late in the 2.5 cycle,
> so it was backed out with the intention of revisiting the idea for 2.6 -
> check the svn log for lib/runpy.py).
>
> The trick with that approach was that runpy.run_module() would *always*
> set "__module_name__" to the real name of the module being executed,
> regardless of what it was told to set "__name__" to.
>
> Then the import machinery was modified such that if it came across an
> explicit relative import being made from a module called "__main__", it
> would check to see if "__module_name__" was set, and if so, use that
> instead.
>

I don't like having to tweak the import machinery for this.  It feels
like working around a problem instead of trying to solve it.  Plus it
doesn't address Steve's concerns of when modules are still named
'__main__' because there absolute name cannot be figured out.

-Brett


More information about the Python-3000 mailing list