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

Christian Heimes lists at cheimes.de
Mon Apr 23 02:35:14 CEST 2007

Neil Toronto wrote:
> On the other hand, IMNSHO, either of the following two are just about 
> perfect in terms of understandability, and parsimony:
>     def __main__():  # we really don't need args here
>         # stuff

I think __main__(*argv) has some benefits over __main__(). It allows you 
to call the function with different arguments from another script or a 
unit test.

def __main__(argv=None):
     if argv is None:
         argv = sys.argv # has the same effect but it is ugly

> FWIW, I don't see a problem with a sys.modules['__main__'] - it would 
> even occasionally be useful - but nobody should be *required* to use an 
> abomination like that for what's clearly a newbie task: determining 
> whether a module is run as a script.

I see the problem in having the same module under two names in 
sys.modules. It may lead to issues (reload?). Also it is not necessary 
to get the main module if we store the dotted name in sys.main. So 
sys.modules[sys.main] would return the main module.


More information about the Python-ideas mailing list