knit_script.knit_script_interpreter.statements.Import_Statement module

Used to import python and other knitscript code into program.

This module provides the Import_Statement class, which handles importing functionality from Python modules, knit script standard library modules, and local knit script files. It supports both direct imports and aliased imports with comprehensive fallback logic for module resolution.

class Import_Statement(parser_node, src, alias=None)[source]

Bases: Statement

Statement that imports a Python or knit script module.

Supports importing Python modules, knit script standard library modules, and local knit script files. Handles both direct imports and aliased imports with comprehensive module resolution that searches multiple locations for the requested module.

The import system follows a priority order: Python modules first, then knit script standard library, then local files, and finally standard library knit script files.

This allows knit script programs to seamlessly integrate with Python libraries while providing access to knit script-specific functionality.

src

Expression representing the module name or path to import.

Type:

Expression

alias

Optional alias name for the imported module.

Type:

Expression | None

Parameters:
__init__(parser_node, src, alias=None)[source]

Initialize an import statement.

Parameters:
  • parser_node (LRStackNode) – The parser node from the abstract syntax tree.

  • src (Expression) – Expression representing the module name or path to import. Must evaluate to a Variable_Expression or Attribute_Accessor_Expression.

  • alias (Expression | None, optional) – Optional alias name for the imported module. If None and src is a Variable_Expression, uses the module name as the alias. Defaults to None.

execute(context)[source]

Execute the import by loading the module and adding it to scope.

Attempts to import in the following order: 1. Python module with the exact name 2. Knit script standard library module 3. Local knit script file 4. Standard library knit script file. Once found, adds the module to the current variable scope with the appropriate name or alias.

Parameters:

context (Knit_Script_Context) – The current execution context to import into.

Raises:
  • ImportError – If src is not a module name or path expression, or if alias is not a valid variable expression.

  • If the module cannot be found in any location after trying all resolution methods.

Return type:

None

__str__()[source]

Return string representation of the import statement.

Returns:

A string showing the source and optional alias.

Return type:

str

__repr__()[source]

Return detailed string representation of the import statement.

Returns:

Same as __str__ for this class.

Return type:

str

property line_number: int

Get the line number of the symbol that generated this statement.

Returns:

The line number where this element appears in the source file.

Return type:

int

property location: Location

Get the location of this symbol in KnitScript file.

Returns:

The location of this symbol in the source file, including file name, line number, and position information.

Return type:

Location