Skip to content
Esc
↑↓navigate↵open⌘Jpreview
Start
On this page

Python Modernization with Autohand Code

Upgrade legacy Python applications to modern Python 3+. Transform Python 2 codebases and modernize frameworks with Autohand Code.

Why modernize Python?

Modern Python offers significant advantages over legacy versions:

  • Enhanced security and ongoing support
  • Better performance and memory efficiency
  • Modern language features (f-strings, type hints, async/await)
  • Improved standard library and third-party ecosystem
  • Long-term support and regular updates

Python codebase assessment

Assess your Python application for modernization readiness:

# Create an AGENTS.md to record your target (Python 3.11)
autohand init

# Generate an upgrade roadmap in read-only plan mode
autohand -p "Assess this Python 2.7 codebase for a move to Python 3.11. Write a roadmap to docs/python-roadmap.md" --plan

# Assessment includes:
# - Python 2/3 compatibility issues
# - Deprecated library usage
# - Framework compatibility
# - Estimated effort by module

Python 2 to Python 3 migration

Automatically migrate Python 2 syntax to Python 3:

# Preview the migration as a patch
autohand -p "Migrate src/ from Python 2.7 to 3.11, preserving behaviour and adding type hints" --patch --output py3.patch
git apply --check py3.patch

# Execute the migration in an isolated git worktree
autohand --worktree python3 -p "Migrate src/ from Python 2.7 to 3.11, preserving behaviour and adding type hints. Run pytest when done"

Example transformations

# Before (Python 2.7)
print "Hello, World!"
x = raw_input("Enter value: ")
print "Type:", type(x)
print "Integer division:", 5/2

def greet(name):
    return "Hello, " + name

Becomes:

# After (Python 3.11)
print("Hello, World!")
x = input("Enter value: ")
print(f"Type: {type(x)}")
print("Integer division:", 5//2)

def greet(name: str) -> str:
    return f"Hello, {name}"

Framework modernization

Migrate between Python frameworks with automated transformations:

# Plan a Django 1.11 to 4.2 upgrade (read-only)
autohand -p "Plan an upgrade from Django 1.11 to 4.2, listing breaking changes per app" --plan

# Migrate Flask to FastAPI with async endpoints in a worktree
autohand --worktree fastapi -p "Port the Flask app in app/ to FastAPI with async endpoints. Run pytest when done"

Django migration example

# Before (Django 1.x)
from django.core.urlresolvers import reverse
from django.utils.encoding import force_unicode

def my_view(request):
    context = {'message': force_unicode('Hello')}
    return render_to_response('template.html', context)

Becomes:

# After (Django 4.x)
from django.urls import reverse

def my_view(request: HttpRequest) -> HttpResponse:
    context = {'message': 'Hello'}
    return render(request, 'template.html', context)

Async/await modernization

Transform synchronous code to modern async patterns:

# Before (Synchronous)
import requests
import time

def fetch_data(urls):
    results = []
    for url in urls:
        response = requests.get(url)
        results.append(response.json())
    return results

Becomes:

# After (Asynchronous)
import aiohttp
import asyncio

async def fetch_data(urls: list[str]) -> list[dict]:
    async with aiohttp.ClientSession() as session:
        tasks = [session.get(url) for url in urls]
        responses = await asyncio.gather(*tasks)
        return [await resp.json() for resp in responses]

Type hints integration

Add modern type hints to improve code quality:

# Before (No type hints)
def process_data(data):
    processed = []
    for item in data:
        if item.get('active'):
            processed.append(transform(item))
    return processed

# After (With type hints)
from typing import List, Dict, Any

def process_data(data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    processed: List[Dict[str, Any]] = []
    for item in data:
        if item.get('active'):
            processed.append(transform(item))
    return processed

Testing and validation

Ensure modernization doesn’t break functionality:

# Generate characterization tests for the legacy code before migrating
autohand -p "Write pytest characterization tests for src/ that pin its current behaviour"

# Validate the migrated code against those tests
pytest

# Run the suite on several interpreters (for example with tox)
tox -e py27,py311

Generated test example

# Generated test (pytest)
def test_process_data_functionality():
    """Test that process_data maintains identical behavior"""
    test_data = [{'id': 1, 'active': True, 'value': 10}]
    
    result = process_data(test_data)
    
    assert len(result) == 1
    assert result[0]['id'] == 1
    assert result[0]['active'] is True

Best practices for Python modernization

Pre-migration preparation

  • Comprehensive testing: Establish >90% test coverage before starting migration
  • Dependency audit: Identify all third-party packages and Python 3 compatibility
  • Environment setup: Create isolated Python 3 development environments
  • Team training: Educate team on Python 3 differences and best practices
  • Documentation review: Update all documentation to reflect Python 3 changes

Migration strategy

  • Incremental approach: Migrate module by module rather than all at once
  • Compatibility libraries: Use libraries like six for gradual migration
  • Feature flags: Implement feature toggles for Python 3 specific features
  • Parallel development: Maintain Python 2 and 3 versions during transition
  • CI/CD integration: Set up automated testing for both Python versions

Code transformation

  • String handling: Convert all string literals to Unicode (Python 3 str)
  • Print function: Replace print statements with print() function calls
  • Integer division: Update division operations for Python 3 behavior
  • Exception handling: Modernize exception syntax and handling patterns
  • Iterator protocols: Update methods to use modern iterator patterns

Framework updates

  • Django migration: Follow Django’s official upgrade guides carefully
  • Flask modernization: Update to modern Flask patterns and extensions
  • Async adoption: Consider FastAPI or Django Async for new features
  • Package updates: Upgrade all packages to Python 3 compatible versions
  • Configuration management: Modernize settings and configuration patterns

Quality assurance

  • Automated testing: Maintain comprehensive test suites throughout migration
  • Static analysis: Use tools like mypy, pylint, and black for code quality
  • Security scanning: Run security vulnerability scans on migrated code
  • Performance testing: Validate that performance doesn’t degrade
  • Integration testing: Test all external integrations thoroughly

Common pitfalls to avoid

  • String/bytes confusion: Be careful with text vs. binary data handling
  • Dictionary methods: Update dict.keys(), dict.values(), dict.items() usage
  • Iterator changes: Understand changes in iterator behavior and methods
  • Exception syntax: Update exception handling syntax and patterns
  • Package compatibility: Verify all third-party packages support Python 3

Success story: Data science platform

A data analytics company migrated their Python 2 scientific computing platform:

  • Scope: 500K lines of Python 2.7 code
  • Target: Python 3.11 + modern data science stack
  • Timeline: 8 months phased migration
  • Results: 40% performance improvement, enhanced security
  • Benefits: Access to modern ML libraries and improved maintainability

Last updated on

Was this helpful?

Send Feedback

Help us improve this documentation page. Your feedback is valuable.

We'll only contact you if we need clarification