> ## 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.

# Python From Import

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

```python theme={"dark"}
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.

```python theme={"dark"}
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.

```python theme={"dark"}
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.

```python theme={"dark"}
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.

```python theme={"dark"}
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:

```python theme={"dark"}
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:

```python theme={"dark"}
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" 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
