CodeNebula
Python📜History & Evolutionbeginner

The 2-to-3 Migration (2008–2020)

Python · History & Evolution · Run with python3

Explanation

For 12 years, Python 2 and Python 3 coexisted. The migration tools (`2to3`, `six`, `future`) let libraries ship code compatible with both. Python 2.7 received security patches until January 1, 2020, when official support ended. The migration cost the ecosystem enormous effort but resulted in a cleaner, more consistent language.

Real-World Scenario

NumPy completed its Python 3 port in 2012; Django in 2013; SciPy and Matplotlib around 2014. By 2019, PyPI had over 90% of the top 360 packages Python-3 compatible. The migration is an object lesson in the cost of breaking changes in popular ecosystems.

Quick Snippet

snippet.py
# The __future__ import pattern used during migration
from __future__ import annotations, division, print_function

# After migration: clean Python 3 only
def process(data: bytes) -> str:
    return data.decode("utf-8")

print(process(b"hello"))  # hello

Full Example

migration_pattern.py
# Compile: python3 migration_pattern.py
# Output:
# hello
# Running on Python 3

from __future__ import annotations

def process(data: bytes) -> str:
    return data.decode("utf-8")

print(process(b"hello"))

import sys
if sys.version_info >= (3, 0):
    print("Running on Python 3")
else:
    print("Running on Python 2")
Run command
python3 migration_pattern.py