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

Cameron Simpson cs at zip.com.au
Mon Aug 10 00:48:41 CEST 2015


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)


More information about the Python-ideas mailing list