Types of Imports in Python
Types of Imports in Python
1. Standard Library Imports
- Purpose: To use modules that are part of Python’s standard library.
- Example:
import math
import os
2. Third-Party Module Imports
3. User-Defined Module Imports
4. Absolute Imports
5. Relative Imports
6. Wildcard Imports
- Purpose: To import all public symbols from a module (not recommended due to namespace pollution).
- Example:
from math import *
7. Selective Imports
8. Aliased Imports
9. Dynamic Imports
10. Lazy Imports (Python 3.7+ with importlib)
Best Practices
- Use explicit imports (
from module import item) over wildcard imports for clarity.
- Avoid circular imports by structuring your code well.
- Use
__all__ in modules to control what gets imported with a wildcard import.
import math
import os
# Third-Party Module Imports
import numpy
import pandas as pd
# User-Defined Module Imports
import my_module
from my_module import my_function
# Absolute Imports
from package.subpackage.module import function
# Relative Imports
from . import module # Current package
from ..subpackage import another_module # Parent package
# Wildcard Imports (not recommended)
from math import *
# Selective Imports
from math import sqrt, pi
# Aliased Imports
import numpy as np
import pandas as pd
# Dynamic Imports
module_name = "math"
math_module = __import__(module_name)
print(math_module.sqrt(16))
# Lazy Imports (Python 3.7+)
import importlib
math = importlib.import_module('math')
print(math.sqrt(16))
Interview angle
- “What happens on
import x?” - Python checks sys.modules first, then searches sys.path, executes the module top to bottom once, and caches it. Repeated imports return the cached module, which is why module-level code runs exactly once.
- “How do you fix a circular import?” - usually by moving the shared piece to a third module, or importing inside the function where it’s needed. A circular import is normally a signal that the module boundary is wrong.
- “Absolute or relative imports?” - absolute for clarity and refactor-safety; relative within a package is acceptable and common. Mixing them inconsistently is what makes packages fragile.