The Hidden Cost of import Statements Nobody Warns You About
Lazy Is Good: The Python performance pattern senior engineers swear by
Your team just deployed a new feature. A Kubernetes pod crashes. The orchestrator spins up a replacement. You’re watching the dashboard.
Five seconds pass. Then eight. Then twelve. Your SLO alert fires. Users are hitting 503s. You frantically check the logs — and find that the pod spent eleven seconds doing nothing but importing libraries before it could serve a single request.
You’ve just been hit by the Import Tax.
The Mindset Shift You Need
Most developers think about performance at runtime — “is my database query fast enough?” — but rarely at import time. The assumption is: imports are instant, or close enough.
They are not.
Python’s import statement is deceptively powerful. When you write import google.cloud.aiplatform at the top of a file, Python doesn’t just find a file and read it. It executes it. Every class defined, every decorator applied, every module-level statement run — all of it happens the moment your process starts, before your app serves a single request.
The mindset shift is this:
Treat your import statements like you treat database connections — don’t open them until you actually need them.
Why This Matters More Than You Think
The Python ecosystem has accumulated some extremely heavy packages. Here’s a rough sense of what some common imports could cost on a cold start:
google-cloud-aiplatform alone drags in grpcio, proto-plus, googleapis-common-protos, shapely, and dozens of protobuf-generated modules. It’s not importing a library — it’s importing a city.
The problem is especially acute in three real-world scenarios:
Microservices with aggressive autoscaling. A new pod spins up under load — exactly when you need it fastest. If startup takes 12 seconds, you’ve made your scaling event worse, not better.
Serverless functions. Cold starts are already a known pain point in AWS Lambda or Google Cloud Functions. Heavy imports can push cold start from 200ms to 8 seconds, effectively making serverless unusable for latency-sensitive workloads.
CLI tools. Nobody wants to wait 4 seconds to see a — help message. Heavy imports in a CLI that only sometimes needs a particular feature punish every user, every invocation.
The Fix: Lazy Loading
The idea is simple: don’t import something until you actually need it.
There are three techniques, from simplest to most powerful.




