> ## 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 Relative Import

A relative import in Python specifies a module or package to be imported relative to the current module's location within a package hierarchy, rather than specifying its absolute path from the project root. It utilizes dot notation to traverse the directory structure.

Relative imports strictly require the `from <module> import <resource>` syntax. Using the standard `import <module>` syntax with dot notation will raise a `SyntaxError`.

## Dot Notation Syntax

The dots indicate the directory level relative to the file containing the import statement:

* `.` (Single dot): The current package.
* `..` (Double dot): The parent package.
* `...` (Triple dot): The grandparent package.

## Syntax Visualization

Consider the following package hierarchy:

```text theme={"dark"}
my_package/
├── __init__.py
├── subpackage_a/
│   ├── __init__.py
│   ├── module_x.py
│   └── module_y.py
└── subpackage_b/
    ├── __init__.py
    └── module_z.py
```

If you are writing code inside `my_package/subpackage_a/module_x.py`, relative imports are structured as follows:

```python theme={"dark"}

# Imports a class from a module in the same directory (subpackage_a)
from .module_y import MyClass


# Imports a function from the parent directory's __init__.py (my_package)
from .. import some_function


# Imports a variable from a sibling package (subpackage_b)
from ..subpackage_b.module_z import SOME_CONSTANT
```

## Internal Mechanics

In modern Python (since PEP 451), the import system relies primarily on the module's `__spec__` attribute—specifically `__spec__.parent`—to resolve relative imports. Older attributes like `__package__` are officially deprecated for this purpose.

When Python encounters a relative import, it calculates the target module's absolute name by traversing up the package hierarchy. It strips levels from the current module's package name (`__spec__.parent`) based on the number of leading dots, and then appends the target module name to construct the absolute import path.

* **1 dot (`.`)**: Strips 0 levels from the parent package.
* **2 dots (`..`)**: Strips 1 level from the parent package.
* **3 dots (`...`)**: Strips 2 levels from the parent package.

```python theme={"dark"}

# Inside my_package/subpackage_a/module_x.py
print(__spec__.parent)  # Output: 'my_package.subpackage_a'

from .module_y import MyClass

# Python resolves '.' to the current package: 'my_package.subpackage_a'

# The absolute import executed internally is:

# from my_package.subpackage_a.module_y import MyClass

from ..subpackage_b.module_z import SOME_CONSTANT

# Python resolves '..' by stripping one level from the current package, yielding 'my_package'

# It then appends the target to form the absolute import:

# from my_package.subpackage_b.module_z import SOME_CONSTANT
```

## Execution Constraints

Because relative imports depend on the `__spec__.parent` attribute to determine the package context, they cannot be used in a module that is executed directly as a top-level script.

If a file is executed directly (e.g., `python module_x.py`), Python treats it as the `__main__` module. In this context, the module's `__spec__` attribute is `None`. Without a `__spec__` to provide the parent package hierarchy, the import system cannot resolve the dot notation, resulting in an `ImportError`:

```text theme={"dark"}
ImportError: attempted relative import with no known parent package
```

To execute a module containing relative imports, it must be run as a module within its package using the `-m` flag. This ensures the Python interpreter constructs the proper `__spec__` before execution:

```bash theme={"dark"}
python -m my_package.subpackage_a.module_x
```

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