[Python-Dev] Can we limit the effects of module execution tosys.modules? (was Fix import errors to have data) (original) (raw)

Robert Brewer fumanchu at amor.org
Wed Jul 28 19:50:05 CEST 2004


Barry Warsaw wrote:

I wonder if we can't define a protocol for executing code at module scope, but only if the module is successfully imported. The idea is that we'd define something like an init() for modules that would only get executed after the module was imported, and if there were any failures in importing, it wouldn't get called.

We'd have to handle chained imports though. I haven't thought through all the implications of that but I can imagine it will get messy. You probably don't want to execute any init()'s until all imports are finished, you probably want to execute them in the order you saw them, and you don't want any exceptions raised in init()'s to be deemed an ImportError worthy of sys.modules deletion.

I'll just note that many large projects do this sort of thing on their own already, because the order of imports/execution is both critical and highly project-specific. If such an init() were to be developed, the primary benefit IMO would be reducing many of those ad-hoc schemes to "one way to do it", which might be worth the effort even if it doesn't "fix" the original issue.

Robert Brewer MIS Amor Ministries fumanchu at amor.org



More information about the Python-Dev mailing list