all Python 3astterm
ast.ImportFrom
imports
The node for from module import name: module is the module name or None, names is a list of alias nodes, level counts the relative-import dots.
Syntax
ast.ImportFrom(module, names, level)
In 'from . import x' module is None, not '.' — the dots go into level, so code that joins module + '.' + name crashes on relative imports. 'from x import *' gives an alias with name='*'. 'from __future__ import annotations' is an ImportFrom too, although the compiler treats it specially.
Examples
import ast
for src in ['from os import path as p', 'from .. import utils', 'from x import *']:
n = ast.parse(src).body[0]
print(repr(n.module), n.level, [a.name for a in n.names])
# → 'os' 0 ['path'] / None 2 ['utils'] / 'x' 0 ['*']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy