Python Initialization Lifecycle and Startup Sequence

Python's initialization lifecycle is the deterministic sequence of operations the CPython interpreter executes to transition from an operating system invocation to a fully functional execution environment. This process involves establishing low-level memory allocators, configuring system and environment variables, constructing the core interpreter and thread states, loading built-in types and foundational modules, configuring search paths via the site module, and ultimately executing the target entry point.

1. Pre-Initialization

The startup sequence begins before the primary runtime is fully active, orchestrated at the C level via Py_PreInitialize. During this phase, Python establishes low-level runtime configurations without relying on a functional Python environment.

2. Core Runtime Initialization

Once pre-initialization is complete, the runtime initializes the internal CPython infrastructure via Py_InitializeFromConfig and internal calls like Py_InitializeCore.

3. Main Interpreter and Module System Setup

With the core structures operational, Python transitions to building the user-facing runtime via _Py_InitializeMain.

4. Path Calculation and Site Configuration

Before user code can execute, the interpreter must resolve filesystem paths and load extended system configurations.

5. Execution Phase and Termination

With the environment fully constructed, the runtime transfers control to the user: