[Python-ideas] PEP for executing a module in a package containing relative imports (original) (raw)

Brett Cannon brett at python.org
Fri Apr 20 19:15:43 CEST 2007


On 4/20/07, Christian Heimes <lists at cheimes.de> wrote:

Brett Cannon schrieb: > When a module is being executed as a script, _main_ will be set > to a true value. For all other modules, _main_ will be set to a > false value. This changes the current idiom of:: > > if name == 'main': > ... > > to:: > > if main: > ... > > The current idiom is not as obvious and could cause confusion for new > programmers. The proposed idiom, though, does not require explaining > why _name_ is set as it is. > > With the proposed solution the convenience of finding out what module > is being executed by examining sys.modules['_main_'] is lost. > To make up for this, the sys module will gain the main > attribute. It will contain a string of the name of the module that is > considered the executing module.

What about import sys if name == sys.main: ... You won't have to introduce a new global module var name and it's easy to understand for newbies and experienced developers. The code is only executed when the name of the current module is equal to the executed main module (sys.main). IMO it's much less PIT...B then introducing main.

True, but it does introduce an import for a module that may never be used if the module is not being executed. That kind of sucks for minor performance reasons.

But what do other people think?

-Brett



More information about the Python-ideas mailing list