Import Strategy
May 30, 2026 | 5 min read
Import at Module Level
from math import sqrt # compiles better
def compute(data):
for x in data:
yield sqrt(x) # uses local sqrtAvoid Imports Inside Loops
for x in data:
import math # slow!
math.sqrt(x)Lazy Imports for Optional Features
def optional_feature():
import optional_module
return optional_module.do()