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 email@example.com