Overview
Porting outdated Python 2 codebases to modern Python 3 standards can be a daunting endeavor without the right automation tooling. The futurize command-line utility addresses this challenge by systematically upgrading legacy syntax while maintaining dual-version runtime compatibility. This practical walkthrough demonstrates how to install the toolchain, prepare a staging area, and execute staged refactoring operations with precise backup controls on Ubuntu Linux.
Setting Up futurize on Ubuntu
Begin by updating the local package repositories and installing both the Python 3 package installer and the official future library suite via the standard APT package manager:
apt-get update -qq && apt-get install -y python3-pip python3-future

The futurize utility serves as an incremental transformation bridge, modifying scripts so they function reliably across both legacy 2.x and contemporary 3.x runtimes. To inspect the active installation and verify command availability, check its release version:
futurize --version
⚙️ Key Execution Flags:
-1,--stage1: Applies cleanups that align code with modern Python 2.6/2.7 idioms without breaking legacy backward compatibility.-2,--stage2: Injects compatibility shims from thefuturemodule to guarantee full Python 3 support.-w,--write: Overwrites target source files directly instead of merely printing unified diffs to standard output.-n,--nobackups: Disables automatic.bakfile generation when paired with the-wswitch.-o,--output-dir: Directs transformed source files into an alternative destination directory.-v,--verbose: Emits detailed debugging output and operational metrics during transformation passes.--version: Displays the installed utility build and exits.

Generating a Legacy Python 2 Test File
Isolate your experimental files inside a dedicated sandbox directory by creating and switching to a new folder:
mkdir -p ~/futurize_demo && cd ~/futurize_demo

Next, produce a synthetic legacy script containing distinct Python 2 idiosyncrasies—specifically statement-style print commands, floor integer division, and obsolete dictionary traversal methods (iteritems()):
printf '#!/usr/bin/env python\nprint "Hello, World!"\nprint 1 / 2\nd = {"a": 1, "b": 2}\nprint d.iteritems()\n' > legacy_sample.py

Review the raw contents of legacy_sample.py to confirm that the deprecated syntax patterns are ready for migration testing:
cat legacy_sample.py

Stage 1: Previewing and Applying Low-Risk Modernization
Navigate into the testing directory before initiating the first refactoring pass:
cd ~/futurize_demo

Execute a dry run of Stage 1 to inspect proposed syntax updates without touching the disk. This safe inspection reveals non-breaking modernization adjustments, such as wrapping print statements in function parentheses:
futurize --stage1 legacy_sample.py

Once satisfied with the suggested refactoring, commit the Stage 1 changes directly to the source file. By default, the tool safeguards your initial code by writing an auxiliary legacy_sample.py.bak backup:
futurize --stage1 -w legacy_sample.py

Display the file again to observe how foundational syntax cleanups were introduced while preserving strict compatibility with existing Python 2 installations:
cat legacy_sample.py

Stage 2: Achieving Native Python 3 Compatibility
Ensure your shell session remains in the active project workspace before initiating deeper transformations:
cd ~/futurize_demo

Proceed with the Stage 2 transformation pass to introduce full Python 3 compliance. In this execution, pair the -w flag with --nobackups to apply the compatibility imports and iterator patches directly without generating redundant backup artifacts:
futurize --stage2 --nobackups -w legacy_sample.py

Audit the rewritten script to examine the newly injected compatibility shims, imported future built-ins, and modernized dictionary iterations:
cat legacy_sample.py

Conclude the migration pipeline by executing the refactored script using the default Python 3 runtime engine to ensure zero regressions:
python3 legacy_sample.py

Leveraging futurize simplifies complex migration pipelines by decoupling low-risk syntax modernization from runtime compatibility shims. Adopting this staged methodology allows development teams to systematically deprecate Python 2 artifacts and run modern workloads with confidence.