Python Glossary Русский

all Python 3astterm

ast.alias

imports

The node for one imported name in import and from ... import. Fields: name, the name as a string, and asname, the alias after as or None.

Syntax

ast.alias(name, asname=None)

import os.path is a single alias with name='os.path', not an Attribute chain. In from . import x the module and relative level are not in alias but in ImportFrom (module=None, level=1), and from m import * gives an alias with name='*'. alias carries lineno positions only since 3.10.

Examples

import ast
tree = ast.parse('import os.path\nfrom math import sqrt as root, pi')
for node in tree.body:
    print([(a.name, a.asname) for a in node.names])
# → [('os.path', None)] / [('sqrt', 'root'), ('pi', None)]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy