class astroid.nodes.Module(name: str, file: str | None = None, path: Sequence[str] | None = None, package: bool = False, pure_python: bool = True)[source]#

Bases: LocalsDictNodeNG

Class representing an ast.Module node.

>>> import astroid
>>> node = astroid.extract_node('import astroid')
>>> node
<Import l.1 at 0x7f23b2e4e5c0>
>>> node.parent
<Module l.0 at 0x7f23b2e4eda0>
absolute_import_activated() bool[source]#

Whether PEP 328 absolute import behaviour has been enabled.


Whether PEP 328 has been enabled.

block_range(lineno: int) tuple[int, int][source]#

Get a range from where this node starts to where this node ends.


lineno – Unused.


The range of line numbers that this node belongs to.

body: list[node_classes.NodeNG]#

The contents of the module.

bool_value(context: InferenceContext | None = None) bool[source]#

Determine the boolean value of this node.


The boolean value of this node. For a Module this is always True.

display_type() str[source]#

A human readable type of this node.


The type of this node.

Return type:


doc_node: Const | None#

The doc node associated with this node.


The path to the file that this ast has been extracted from.

This will be None when the representation has been built from a built-in module.

file_bytes: str | bytes | None = None#

The string/bytes that this ast was built from.

file_encoding: str | None = None#

The encoding of the source file.

This is used to get unicode out of a source file. Python 2 only.

frame(*, future: Literal[None, True] = None) _T[source]#

The node’s frame node.

A frame node is a Module, FunctionDef, ClassDef or Lambda.


The node itself.

fully_defined() bool[source]#

Check if this module has been build from a .py file.

If so, the module contains a complete representation, including the code.


Whether the module has been built from a .py file.

future_imports: set[str]#

The imports from __future__.


Get the child nodes below this node.

getattr(name, context: InferenceContext | None = None, ignore_locals=False)[source]#
globals: dict[str, list[InferenceResult]]#

A map of the name of a global variable to the node defining the global.

igetattr(name: str, context: InferenceContext | None = None) Iterator[InferenceResult][source]#

Infer the possible values of the given variable.


name – The name of the variable to infer.


The inferred possible values.

import_module(modname: str, relative_only: bool = False, level: int | None = None, use_cache: bool = True) Module[source]#

Get the ast for a given module as if imported from this module.

  • modname – The name of the module to “import”.

  • relative_only – Whether to only consider relative imports.

  • level – The level of relative import.

  • use_cache – Whether to use the astroid_cache of modules.


The imported module ast.

locals: dict[str, list[InferenceResult]]#

A map of the name of a local variable to the node defining the local.


The name of the module.


The next sibling statement node.


The next sibling statement node.

Return type:

NodeNG or None


Whether the node represents a package or a module.

postinit(body: list[NodeNG], *, doc_node: Const | None = None)[source]#

The previous sibling statement.


The previous sibling statement node.

Return type:

NodeNG or None


The list of the names that are publicly available in this module.


The list of public names.

Return type:



Whether the ast was built from source.

pytype() Literal['builtins.module'][source]#

Get the name of the type that this node represents.


The name of the type.

relative_to_absolute_name(modname: str, level: int | None) str[source]#

Get the absolute module name for a relative import.

The relative import can be implicit or explicit.

  • modname – The module name to convert.

  • level – The level of relative import.


The absolute module name.


TooManyLevelsError – When the relative import refers to a module too far above this one.

scope_attrs: ClassVar[set[str]] = {'__doc__', '__file__', '__name__', '__package__', '__path__'}#

The names of module attributes available through the global scope.

scope_lookup(node: LookupMixIn, name: str, offset: int = 0) tuple[LocalsDictNodeNG, list[node_classes.NodeNG]][source]#

Lookup where the given variable is assigned.

  • node – The node to look for assignments up to. Any assignments after the given node are ignored.

  • name – The name of the variable to find assignments for.

  • offset – The line offset to filter statements up to.


This scope node and the list of assignments associated to the given name according to the scope where it has been found (locals, globals or builtin).


The names of special attributes that this module has.

statement(*, future: Literal[None, True] = None) NoReturn[source]#

The first parent node, including self, marked as statement node.

When called on a Module this raises a StatementMissing.


Get a stream to the underlying file or bytes.


file or io.BytesIO or None


The list of imported names when this module is ‘wildcard imported’.

It doesn’t include the ‘__builtins__’ name which is added by the current CPython implementation of wildcard imports.


The list of imported names.

Return type: