Book Review: Porting to Python 3

Lennart Regebro sent me a copy of his Porting to Python 3 following
PyCon 2011, and now that work on my own book is nearly complete I was
finally able to sit down and study it. I have two major Python 3
porting projects ahead of me soon, and I hoped that Lennart’s book
would guide those projects. I was not disappointed.

Quick Review

Inspired by Gene Ambaum and Bill Barnes’ review short-cuts, here’s my
review for the impatient reader:

Why I picked it up: I am preparing to port PyMOTW to Python 3
and
we are starting to study what it would take to make the switch at
work, too.

Why I finished it: At a computationally pleasing 128 pages, the
book gets right to the point, without a lot of fluff and filler.

I’d give it to: Developers with enough Python 2 code that porting
to version 3 is going to be a major project. Programmers who want to
support both 2 and 3 at the same time will also find helpful
information.

Python 2 vs. 3

The Python community is in an interesting period of change. Major
development of Python 2 has stopped with release 2.7, but there are
still a lot of developers out in the world using it as their primary
tool. The number of packages available under Python 3 continues to
grow steadily, but there is still a lot of legacy code to be ported,
supported, or simply cut off. Lennart Regebro’s new book, Porting to
Python 3
, is a guide for those of us who have not yet completed our
transition. It covers the options for supporting Python 3, either
concurrently or exclusively, and includes guidance about choosing the
best approach for your project.

Just the Facts

While there is a lot to like about Porting to Python 3, the two
aspects that stand out are the size and the organization of the
content. The author does not beat around the bush or belabor his
point. His writing is clear and concise, resulting in a slim handbook
that can be given to a manager who needs to understand the
ramifications of moving from Python 2 to Python 3, while still
containing enough detail to be used by a developer for reference
during the porting process.

The author does not beat around the bush or belabor his point.

Strategies for Successful Migration

The material in the book is organized in exactly the way you need to
digest it. The first chapter is a quick explanation of what Python 3
is, with guidance for deciding when, and whether, to make the
switch. Chapter two covers strategies for migrating, either all at
once or during installation so that both Python 2 and 3 can be
supported with the same code base.

Next, the author helps you prepare for the porting effort by providing
tips for bringing your code into the best beginning state. He covers
the process for staging the migration by getting the code to run
correctly under Python 2.7, possibly with a compatibility layer so
that the same source can run under Python 3 as well.

The author helps you prepare by providing tips for bringing your
code into the best beginning state.

Chapter 4 introduces 2to3, the primary tool for automatically
porting Python 2 code to run under Python 3. 2to3 supports a large
number of conversions, but no automatic solution is going to be
perfect, so chapter 5 explains how to address the most commonly found
cases where the tool may have made the wrong decision or where it
could not translate the code at all.

Chapter 6 lists the cases where using more “modern idioms” will either
make the code easier to maintain for both Python 2 and 3, or make it
run more cleanly under Python 3.

Advanced Topics

The rest of the chapters are about more advanced topics, including
techniques for supporting Python 2 and 3 without using 2to3; changes
in the C API that affect extension modules; and customizing 2to3 to
handle cases that appear in your code, but are not common enough to
already be addressed by the core library.

Handy Reference

The appendices provide concise reference list of common porting issues
and their solutions, so it is not necessary to search the entire book
for a fix when you encounter a problem. There is also a list of the
standard library modules that have been renamed or otherwise
reorganized, something I will find especially useful while working on
PyMOTW.

Conclusions

Porting to Python 3 is self-published, and that shows in some of the
rough edges. But those do not detract from the value of the
information and advice that it contains. I recommend it for developers
preparing to migrate their code base, especially if there is enough
code to make it a major project. Programmers who want to support both
2 and 3 at the same time will also find helpful information.

Sample Chapter

The website for the book has chapter two, Migration
Strategies
, available for free.

Disclaimer: I received a free review copy of this book from the
author.