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

Cameron Simpson cs at zip.com.au
Sun Aug 9 12:34:49 CEST 2015


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.

I just did a quick test package named "testmod" via "python -m testmod" and:

- __init__.py has the __name__ "testmod"
- __main__.py has the __name__ "__main__"

in both python 2.7 and python 3.4.

Since my test script reports:

  % python3.4 -m testmod
  __init__.py: /Users/cameron/rc/python/testmod/__init__.py testmod
  __main__.py: /Users/cameron/rc/python/testmod/__main__.py __main__
  % python2.7 -m testmod
  ('__init__.py:', '/Users/cameron/rc/python/testmod/__init__.pyc', 'testmod')
  ('__main__.py:', '/Users/cameron/rc/python/testmod/__main__.py', '__main__')

would it be enough to say that this change should only apply if the module is 
not a package?

I'll do some more fiddling to see exactly what happens in packages when I 
import pieces of them, too.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Python-ideas mailing list