[Python-3000] PEP to change how the main module is delineated (original) (raw)

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 "modulename" 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.runmodule() would always set "modulename" 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 "modulename" 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