[Python-ideas] PEP for executing a module in a package containing relative imports (original) (raw)

Brett Cannon brett at python.org
Fri Apr 20 19:09:55 CEST 2007


On 4/19/07, Steven Bethard <steven.bethard at gmail.com> wrote:

On 4/19/07, Brett Cannon <brett at python.org> wrote: > Let me know what you think. I especially want to hear which proposal > people prefer; the one in the PEP or the one in the Open Issues > section. Plus I wouldn't mind suggestions on a title for this PEP.

As you've probably already guessed, I prefer the:: if main: version. I don't think I've ever used sys.modules['main'].

Yeah, I figured you would. =)

> Transition Plan > =============== > > Using this solution will not work directly in Python 2.6. Code is > dependent upon the semantics of having _name_ set to > '_main_'. There is also the issue of pre-existing global > variables in a module named _main_.

Could you explain a bit why main couldn't be inserted into modules before the module is actually executed? E.g. something like:: _>>> moduletext = '''_ ... main = 'foo' ... print main ... ''' >>> import new >>> mod = new.module('mod') >>> mod.main = True >>> exec moduletext in mod.dict foo >>> mod.main 'foo' I would have thought that if Python inserted main before any of the module contents got exec'd, it would be backwards compatible because any use of main would just overwrite the default one.

That's right, and that is the problem. That would mean if main was false but then overwritten by a function or something, it suddenly became true. It isn't a problem in terms of whether the code will run, but whether the expected semantics will occur.

-Brett



More information about the Python-ideas mailing list