`python-modernize`: Automating Dual Python 2/3 Codebases

`python-modernize` is a tool built on `lib2to3` (or `fissix`) that automatically rewrites Python 2 code to be compatible with both Python 2.6+ and Python 3, often leveraging `six` as a runtime dependency for dual-version support, unlike `2to3`'s one-way conversion.

The top 3

  1. python-modernize's Core: 2to3 with Six Integration: python-modernize acts as a wrapper around the `2to3` utility, but crucially integrates the `six` library at runtime to enable a single codebase to run on both Python 2 and Python 3, unlike `2to3` which is a one-way conversion.
  2. Key Fixers: Text Literals & Python 2.6+ Compatibility: By default, `python-modernize` leverages fixers to modernize Python 2 code, notably handling Unicode literals by not changing them for Python 3.3+ compatibility or by wrapping them with `six.u()` for Python 3.1/3.2, ensuring compatibility with Python 2.6 and newer versions.
  3. Limitations: Manual Review for Inefficiencies & Edge Cases: While `python-modernize` automates much of the Python 2 to 3 migration, it doesn't guarantee perfect or efficient conversions for all cases, often requiring manual review and adjustments for visibly inefficient code or complex refactoring beyond its automated fixers.

Sources

Open the full topic