Relative Import Configuration

Console Output
>

Relative Visualization

Click "Generate Relative Import" button in the left panel to see the visualization
Same Directory Relative Import
Import from current directory level
current_file.py
Current file
math module
Target module
sqrt(16)
Function call
Import Syntax:
from .math import sqrt
. = current directory
.. = parent directory
... = grandparent directory
Directory Structure Example:
project/
├── main.py
├── utils/
├── __init__.py
├── math.py
└── helper.py
├── models/
├── __init__.py
└── user.py
└── services/
├── __init__.py
└── data_service.py
Relative import examples:
• From main.py: from .utils.math import sqrt
• From utils/helper.py: from ..models.user import User
• From services/data_service.py: from ...utils.helper import process_data
Relative vs Absolute Import Comparison:
Relative Import
from .math import sqrt
Pros:
  • Shorter syntax
  • Package independence
  • Easier refactoring
Cons:
  • Harder to understand
  • Can be confusing
Absolute Import
from mypackage.math import sqrt
Pros:
  • Clear and explicit
  • Easier to understand
  • IDE support
Cons:
  • Longer syntax
  • Package dependent
  • Harder to refactor
Best Practices for Relative Imports:
Relative Import Guidelines
• Use relative imports only within the same package
• Keep relative imports simple (max 2-3 dots)
• Use __init__.py files to make directories packages
• Consider package structure when using relative imports
• Use absolute imports for external packages
• Be consistent with import style throughout the project
Common Pitfalls
• Running scripts directly with relative imports
• Using too many dots in relative imports
• Mixing relative and absolute imports inconsistently
• Not having __init__.py files in package directories
• Using relative imports outside of packages