[Python-ideas] PEP: Distributing a Subset of the Standard Library
Chris Angelico
rosuav at gmail.com
Mon Nov 28 17:52:21 EST 2016
On Tue, Nov 29, 2016 at 9:19 AM, Nathaniel Smith <njs at pobox.com> wrote:
> This also suggests that the overall error-handling flow for 'import
> foo' should look like:
>
> 1) run foo.missing.py
> 2) if it raises an exception: propagate that
> 3) otherwise, if sys.modules["foo"] is missing: raise some variety of
> ImportError.
> 4) otherwise, use sys.modules["foo"] as the object that should be
> bound to 'foo' in the original invoker's namespace
+1, because this also provides a coherent way to reword the try/except
import idiom:
# Current idiom
# somefile.py
try:
import foo
except ImportError:
import subst_foo as foo
# New idiom:
# foo.missing.py
import subst_foo as foo
import sys; sys.modules["foo"] = foo
#somefile.py
import foo
ChrisA
More information about the Python-ideas
mailing list