[Python-ideas] PEP for executing a module in a package containing relative imports

Christian Heimes lists at cheimes.de
Sat Apr 21 16:25:57 CEST 2007

Steven Bethard wrote:
> But you have to understand a few things to understand why this works.
> You have to know that __name__ is the name of the module, and that if
> you want to find out the name of the main module, you need to look at
> sys.main.  With the idiom::
>     if __main__:
> all you need to know is that the main module has __main__ set to true.
>> IMO it's much less PIT...B then introducing __main__.
> Could you elaborate? Do you think it would be hard to introduce
> another module-level attribute (like we already do for __name__)? Or
> do you think that the code would be hard to maintain? Or something
> else...?

This is just my humble opinion. I'm new to Python core development.

Well, in my opinion a new module level var like __main__ isn't worth to 
add when it is just boolean flag. With the proposed addition of sys.main 
the same information is available with just few more characters to type. 
If I recall correctly Python is trying to get rid of global variables in 
Python 3000.

I don't think it's hard to add - even for me although I know less about 
the Python core. I'm more worried about the side effect when people have 
already used __main__ as a function. The problem is in 2to3.

If you like to introduce __main__ why not implement 
http://www.python.org/dev/peps/pep-0299 ? It proposes a __main__(*argv*) 
module level function that replaced the "if __name__ == '__main__'" 
idiom. The __main__ function follows the example of other programming 
languages like C, C# and Java.

I'm aware of the fact that the PEP was rejected but I think it's worth 
to discuss it again.


More information about the Python-ideas mailing list