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

Chris Angelico rosuav at gmail.com
Sat Aug 8 12:30:25 CEST 2015


On Sat, Aug 8, 2015 at 7:49 PM, Cameron Simpson <cs at zip.com.au> wrote:
> The raw text of the PEP is below. It feels uncontroversial to me, but then
> it would:-)
>

I'm not sure that it'll be uncontroversial, but I agree with it :)

The risk that I see (as I mentioned in the previous thread, but
reiterating for those who just came in) is that it becomes possible to
import something whose __name__ is not what you imported. Currently,
you can "import math" and see that math.__name__ is "math", or "import
urllib.parse" and, as you'd expect, urllib.parse.__name__ is
"urllib.parse". In the few cases where it isn't exactly what you
imported, it's the canonical name for it - for instance,
os.path.__name__ is posixpath on my system. The change proposed here
means that the canonical name for the module you're running as the
main file is now "__main__", and not whatever else it would have been.

Consequences for pickle/multiprocessing/Windows are mentioned in the
PEP. Are there any other places where a module's name is checked?

ChrisA


More information about the Python-ideas mailing list