[3.8] bpo-37409: fix relative import with no parent (GH-14956) #15913
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Relative imports use resolve_name to get the absolute target name,
which first seeks the current module's absolute package name from the globals:
If package (and spec.parent) are missing then
import uses name, truncating the last segment if
the module is a submodule rather than a package init.py
(which it guesses from whether path is defined).
The name attempt should fail if there is no parent package (top level modules),
if name is 'main' (-m entry points), or both (scripts).
That is, if both name has no subcomponents and the module does not seem
to be a package init module then import should fail..
(cherry picked from commit 92420b3)
Co-authored-by: Ben Lewis benjimin@users.noreply.github.com
https://bugs.python.org/issue37409
Automerge-Triggered-By: @brettcannon