mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Swap 'if' branches so content matches to condition in importlib example (GH-14947)
Prior to this change the guard on an 'elif' used an assignment expression whose value was used in a later 'else' block, causing some confusion for people.
(Discussion on Twitter: https://twitter.com/brettsky/status/1153861041068994566.)
Automerge-Triggered-By: @brettcannon
(cherry picked from commit 544fa15ea1
)
Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com>
This commit is contained in:
parent
25cb4fd4fb
commit
c594c274e9
1 changed files with 3 additions and 3 deletions
|
@ -1640,14 +1640,14 @@ import, then you should use :func:`importlib.util.find_spec`.
|
||||||
|
|
||||||
if name in sys.modules:
|
if name in sys.modules:
|
||||||
print(f"{name!r} already in sys.modules")
|
print(f"{name!r} already in sys.modules")
|
||||||
elif (spec := importlib.util.find_spec(name)) is None:
|
elif (spec := importlib.util.find_spec(name)) is not None:
|
||||||
print(f"can't find the {name!r} module")
|
|
||||||
else:
|
|
||||||
# If you chose to perform the actual import ...
|
# If you chose to perform the actual import ...
|
||||||
module = importlib.util.module_from_spec(spec)
|
module = importlib.util.module_from_spec(spec)
|
||||||
sys.modules[name] = module
|
sys.modules[name] = module
|
||||||
spec.loader.exec_module(module)
|
spec.loader.exec_module(module)
|
||||||
print(f"{name!r} has been imported")
|
print(f"{name!r} has been imported")
|
||||||
|
else:
|
||||||
|
print(f"can't find the {name!r} module")
|
||||||
|
|
||||||
|
|
||||||
Importing a source file directly
|
Importing a source file directly
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue