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

Nick Coghlan ncoghlan at gmail.com
Mon Apr 23 11:31:39 CEST 2007


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.

The issue discussed in PEP 338 (and this new PEP) gets fixed, with zero 
impact on anything else (even normal import speed is unaffected, as the 
check for "__module_name__" would only occur on an error handling path 
that currently raises an exception).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list