Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The from ... import ... statement in Python binds specific attributes, classes, functions, or submodules from a target module directly into the importing scope’s namespace. Unlike the standard import statement, which binds the module object itself to a local name, from import extracts the specified identifiers, allowing them to be referenced without fully qualified module dot-notation.

Syntax

from <module_name> import <identifier> [as <alias>] [, <identifier> [as <alias>] ...]

Execution Mechanics

When the Python interpreter encounters a from import statement, it executes the following sequence:
  1. Resolution: Locates the specified <module_name>.
  2. Initialization: Compiles and executes the target module if it is not already cached in sys.modules.
  3. Extraction: Looks up the specified <identifier> within the target module’s namespace (__dict__). If the identifier is not found in the module’s namespace, Python’s import system actively attempts to resolve and load a submodule with that exact name. If this secondary resolution also fails, it raises an ImportError.
  4. Binding: Creates a reference to the resolved object in the importing scope’s namespace. The target module object itself is not bound to the importing namespace.

Variations

Single and Multiple Identifiers You can import one or multiple specific identifiers from a module. Multiple identifiers are comma-separated. Parentheses can be used to format multi-line imports.
from math import pi
from collections import deque, defaultdict

from typing import (
    List,
    Dict,
    Optional,
    Union
)
Aliasing (as keyword) The as keyword binds the imported object to a different identifier in the importing scope. This is mechanically used to prevent namespace collisions or to conform to local naming conventions.
from datetime import datetime as dt
Wildcard Import (*) The wildcard syntax binds all public names defined in the target module directly into the importing module’s global namespace. The interpreter determines public names by inspecting the target module’s __all__ attribute (a list of strings defining the public API). If __all__ is not defined, it imports all identifiers that do not begin with a single underscore (_). Restriction: In Python 3, wildcard imports are strictly prohibited inside local scopes, such as within functions or classes. Attempting to execute from ... import * inside a function will raise a SyntaxError. They are exclusively permitted at the module level.
from math import *
Relative Imports Within a package hierarchy, from import supports relative imports using dot notation. A single dot (.) represents the current package directory, while multiple dots (.., ...) traverse up the parent directories. Relative imports strictly require the from keyword; import .module is invalid syntax.
from . import sibling_module
from ..parent_package import shared_function
from .subpackage.module import SpecificClass

Namespace and Reference Implications

Because from import creates a direct reference to the target object in the importing scope, it behaves differently than module-level imports regarding reassignment:
from config import settings


# This rebinds the identifier 'settings' in the importing scope to a new dictionary.

# It does NOT modify the 'settings' object inside the 'config' module.
settings = {"new": "value"} 
However, if the imported identifier points to a mutable object, mutating the object locally will reflect globally across all modules referencing that object. This occurs because the identifier in the importing scope and the identifier in the target module point to the exact same memory address:
from config import settings


# This mutates the underlying dictionary.

# The change IS reflected in the 'config' module and anywhere else it is imported.
settings["key"] = "new_value" 
Master Python with Deep Grasping Methodology!Learn More