Migrating Python 2 to Python 3

Python 3 has been out for over a decade and is widely used around the world. In this course, you will learn how to migrate from Python 2 to Python 3 and how to benefit from Python 3's improved features and performance.
Course info
Level
Intermediate
Updated
Mar 5, 2018
Duration
1h 52m
Table of contents
Course Overview
Why Move to Python 3?
Porting Strategies
Changes to Core Types
Standard Library Changes
Moving Away from Python 2
Description
Course info
Level
Intermediate
Updated
Mar 5, 2018
Duration
1h 52m
Description

At the core of a successful migration from Python 2 to 3 is a thorough knowledge of the differences, the strategies, and common pitfalls. In Migrating Python 2 to 3, you will learn how to assess your application and phase a migration without losing functionality or performance. First, you will learn what changed in Python 3 and how to update Python 2 code to be compatible. Next, you will explore strategies for migrating an application and how to phase the upgrade. Finally, you will discover how to leverage Python 3 features to accelerate the performance of your application. When you are finished with this course, you will have a foundational knowledge of Python 3 that will help you as you move forward to migrate any application from Python 2 to 3.

About the author
About the author

Open-Source advocate and Apache Software Foundation member

More from the author
What's New in Python 3.7
Intermediate
56m
1 Jun 2018
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone. My name is Anthony Shaw, and welcome to my course, Migrating Python 2 to Python 3. I'm a Python enthusiast at the Apache Software Foundation. Python 3 has been out for over a decade now, and still the majority of production applications are running on Python 2. Python 3 is faster. It has some amazing new features, like asynchronous programming and type hinting. In this course, we're going to migrate a Python 2 application to Python 3 the right way. Some of the major topics that we will cover include strategies for porting, changes to the core types in Python 3, automated tools and where to use them, and leveraging Python 3 features. By the end of this course, you will know how to migrate any application, big or small, to Python 3. Before beginning the course, you should be familiar with the basics of Python and Python 2, and from here, you should feel comfortable diving into Python 3 with courses on concurrency and asyncio. I hope you'll join me on this journey to learn Python 3 migrations with the Migrating Python 2 to 3 course at Pluralsight.

Why Move to Python 3?
Hi. I'm Anthony Shaw, and I'm going to take you on an exciting journey into the future of Python. In this course, I will show you how to become a Python 2 to 3 migration master. Python 2 is over; 2. 7 was the last major release. There are no features being added to Python 2, and more and more we're seeing major libraries and frameworks dropping support for Python 2. Before your application becomes legacy, you need to migrate, but how, and how long is it going to take? Do I need to rewrite everything? Can I just use an automated tool like 2to3? We'll go through the fundamentals of what changed in Python 3, upgrade a simple e-commerce application, and I'll show you a few tricks from my experience in writing and supporting Python 3 applications and libraries.

Porting Strategies
In this module, we're going to explore 3 viable strategies for moving to Python 3. I'll show you some pros and cons of each approach and let you decide what strategy will work for you and your project. The first option maybe sounds the easiest. That's just start fresh. Let's just throw away our existing code and start again in Python 3. The second option is to use some of the automated tooling out there for updating your code to Python 3. The last is a segmented approach of upgrading a module at a time. In this module, we'll talk about the different approaches out there, pros and cons, and then present a balanced approach. Using testing to validate compatibility when using a portability library as well, can simplify differences and aid migration. This module will also talk about the role of testing in migrations, introduce some tooling for test automation, and we'll use a tool called tox to test compatibility between Python versions. I will also introduce the dashboard for showing compatibility that will be used throughout the course.

Changes to Core Types
Of all the changes in Python 3, the farthest reaching are the changes to core types. These changes are so fundamental they most likely impact all of your existing modules and submodules. It's important to understand the nature of these changes and patterns for solving them. Store them in your muscle memory, and you can spot where you're going to have these issues in the future. The first and largest change is the strings. Python 3 introduced a revolutionary change to text and data types. The idea was to better support Unicode and avoid errors when encountering special characters. The consequence though is that your existing Python 2 code will likely not work with Python 3 if it deals with strings. Whilst dictionaries behave in the same way, the interfaces for iterating over them have changed, which often causes challenges. And lastly, Python 3 more naturally supports long integers, so we'll go into that change as well.

Moving Away from Python 2
In this final module, we will explore what happens when you move away from Python 2. Having a pure Python 3 implementation of your app means you can finally take advantage of some of the new features in Python 3. Breaking down what those are, we have new syntax. There are new keywords, async and await. There's new string formatting methods. There's type hinting. There are new modules for our statistics, secrets, path exploration, and also there's new operators for working with matrices. Asynchronous programming is a whole new area in Python 3. When Python 3 first came out, the focus and effort was on stability and bringing the standard library across to the major differences in types with strings and integers. This kept the Python team busy for releases 3. 0 to 3. 3, and in the release of 3. 4 we started to see some new functionality arise in the Python 3 family. One of the most confusing parts of what is new in Python 3 is comparing 2. 7 with all of the dot releases in 3; otherwise, you don't get a full list of improvements. We've explored many of those in this course, but my assumption is that you'll be migrating to at least 3. 5, if not 3. 6. The effort involved in initial Python 3 migrations is temporary. Once you settle on Python 3, there have been no new breaking changes past 3. 4, and new versions of Python 3 released will be backward compatible with your new application. First, I'd like to talk about performance. If your application is performance critical, it's going to be important to note the differences between the Python 3 versions.