Steven Lott reviews the central issues encountered in migrating from Python 2.7 to Python 3. While tools can do a lot, there are still edge and corner cases that require careful thought and planning. Learn about the areas of the language that have changed.
- [Instructor] In this course, I want to show you several ways to get Python code from version two to version three. The benefits of using Python 3 are some simplifications and a considerable boost in speed. In this chapter, I'm going to share several strategies for making the transition. I'm also going to show a number of tools that can help automate the process. First, the word migration. My focus will be on ways to preserve all the features of the software while changing the implementation from the older version of Python to the new state-of-the-art Python 3.
The important goal is to be sure the application does the same thing in version three that it did in Python 2. Of course, it will then be faster and have fewer quirks and can now grow and evolve with the rest of the Python ecosystem. I'm going to start with an overview of the essential changes between version two and version three of the Python language. There's a short list of the most commonly encountered changes.
The Feature Comparison chapter will provide a detailed list of featured changes. I'll show a few examples of some of these changes. And the three basic strategies are Convert, Coordinate, and Coexist. I'll describe how the strategies work and how to choose among them. The bulk of the course will focus on the tools available for migrating code to Python 3. We'll look at three approaches.
The Six Module provides an avenue to create a new module that coexists in Python 2 and Python 3 environments. This can be complex and I want to present it first to make it clear why this isn't always the best approach. The Python-Future Project provides some tools that leverage Six. This project can help with coordination or conversion strategies.
The 2to3 Module provides an automated conversion. The Modernized Project also provides some handy automation tools. Each of these requires some preparation to be sure that the automated conversion really works. And I want to finish with a review of the Simplest Approach. Spoiler alert. Doing a manual rewrite into Python 3 often produces the best quality software with only moderate amount of work.
Why is this so hard? The languages really are different as we'll see in the rest of this chapter and chapter two, there are some profound differences. The syntax has changed in a few places and while the bulk of the languages are the same, a few statements have been revised. The semantics have changed in ways that lead to profound internal simplifications and this in turn, leads to much faster processing.
It turns out there's no trivial mapping between the two languages. Some of the Python 2 ambiguities regarding Unicode and Bytes may require careful rework. Converting from Python 2 to Python 3 isn't something that can always be done by an ignorant piece of software. The conversion may require a person's hand on the wheel to choose the best route for the migration.
There may be design rework and this may require some thinking and experimenting to make the best use of the new features and of course, there'll be a lot of testing; both unit testing of the code in isolation and integration testing of the entire library or application. I want to start with a quick overview of where we are today and what's involved in migrating to Python 3.
- Reviewing the differences between the two versions
- Reviewing the syntax changes introduced with Python 3
- Understanding the changes to built-in functions
- Reviewing the most important changes to the Python standard library
- Understanding which tools are required to migrate from Python 2.7 to Python 3
- Using six to handle class definitions
- Using six with standard library changes
- Using future
- Making syntax changes and class changes with futurize
- Using 2to3 or modernize