[Python-ideas] proposal: "python -m foo" should bind sys.modules['foo']

Cameron Simpson cs at zip.com.au
Thu Aug 6 02:07:53 CEST 2015


On 06Aug2015 00:58, Nick Coghlan <ncoghlan at gmail.com> wrote:
>On 5 August 2015 at 19:01, Cameron Simpson <cs at zip.com.au> wrote:
>> Ah, ok, many thanks. I've now read this, particularly this section:
>> http://legacy.python.org/dev/peps/pep-0395/#fixing-pickling-without-breaking-introspection
[...]
>From an *implementation* perspective, you'll want to look at Eric's
>own PEP 451: https://www.python.org/dev/peps/pep-0451/

Ah. Yes. Thanks. On that basis I withdraw my .__qualname__ suggestion because 
there exists module.__spec__.name. So it now reduces the proposal to making the 
-m option do this:

 % python -m module.name ...

runs (framed loosely like 
https://www.python.org/dev/peps/pep-0451/#how-loading-will-work):

 # pseudocode, with values hardwired for clarity
 import sys
 module = ModuleType('module.name')
 module.__name__ = '__main__'
 sys.modules['__main__'] = module
 sys.modules['module.name'] = module
 ... load module code ...

I suspect "How Reloading Will Work" would need to track both module.__name__ 
and module.__spec__.name to reattach the module to both entires in sys.modules.

>In particular, __spec__.name already holds the additional state we
>need to fix this behaviour (i.e. the original module name), I just
>haven't found the opportunity to go back and update runpy to take
>advantage of PEP 451 to address this and other limitations.
>It would definitely be good to have a PEP addressing that.

I'd like to have a go at addressing just the change I outline above, in the 
interests of just getting it done. Is that too narrow a change or PEP topic?  
Are there specific other things I should be considering/addressing that might 
be affected by my suggestion?

Also, where do I find to source for runpy to preruse?

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

A program in conformance will not tend to stay in conformance, because even if
it doesn't change, the standard will.   - Norman Diamond <diamond at jit.dec.com>


More information about the Python-ideas mailing list