[Python-ideas] PEP-499: "python -m foo" should bind to both "__main__" and "foo" in sys.modules

Joseph Jevnik joejev at gmail.com
Mon Aug 10 01:33:38 CEST 2015


I would be okay if this change did not affect execution of a package with
the python -m flag. I was only concerned because a __main__ in a package is
common and wanted to make sure you had addressed it.

On Sun, Aug 9, 2015 at 6:48 PM, Cameron Simpson <cs at zip.com.au> wrote:

> On 09Aug2015 20:34, Cameron Simpson <cs at zip.com.au> wrote:
>
>> On 09Aug2015 03:05, Joseph Jevnik <joejev at gmail.com> wrote:
>>
>>> If I have a package that defines both a __main__ and a __init__, then
>>> your
>>> change would bind the __main__ to the name instead of the __init__. That
>>> seems incorrect.
>>>
>>
>> Yes. Yes it does.
>>
> [...]
>
>> would it be enough to say that this change should only apply if the
>> module is not a package?
>>
>
> I append the code for my testmod below, being an __init__.py and a
> __main__.py.  A run shows:
>
>  % python3.4 -m testmod
>  __init__.py: /Users/cameron/rc/python/testmod/__init__.py testmod testmod
>  __main__.py: /Users/cameron/rc/python/testmod/__main__.py __main__
> testmod.__main__
>  __main__ <module 'testmod.__main__' from
> '/Users/cameron/rc/python/testmod/__main__.py'>
>  testmod <module 'testmod' from
> '/Users/cameron/rc/python/testmod/__init__.py'>
>
> (4 lines, should your mailer fold the output.)
>
> It seems to me that Python already does the "right thing" for packages,
> and it is only non-package modules which need the change proposed by the
> PEP.
>
> Comments please?
>
> Code below.
>
> Cheers,
> Cameron Simpson <cs at zip.com.au>
>
> testmod/__init__.py:
>    #!/usr/bin/python
>    print('__init__.py:', __file__, __name__, __spec__.name)
>
> testmod/__main__.py:
>    #!/usr/bin/python
>    import pprint
>    import sys
>    print('__main__.py:', __file__, __name__, __spec__.name)
>    for modname, mod in sorted(sys.modules.items()):
>      rmod = repr(mod)
>      if 'testmod' in modname or 'testmod' in rmod:
>        print(modname, rmod)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150809/fb22429f/attachment.html>


More information about the Python-ideas mailing list