O'Reilly logo
live online training icon Live Online training

Porting from Python 2 to Python 3

Tips and techniques for a smooth transition

Ben Straub

A lot of software is being written in Python 2, but the clock is ticking, and the need to move to Python 3 grows ever greater. As of 2020, Python 2 will no longer be supported. Most Python 2 code won’t run under Python 3, and in most cases, a complete rewrite of your code is just not practical.

In this course, you’ll learn how to make the move to Python 3 smoothly and painlessly. You’ll learn what has changed and how to make your transition step-by-step with help from automation. You’ll leave the course with a general strategy and practical knowledge for moving to Python 3.

What you'll learn-and how you can apply it

By the end of this live, hands-on course, you will understand:

  • What has changed in Python 3
  • General strategies needed to move to Python 3
  • How Unicode works in Python 2 and 3
  • The difference between a ported program and a polyglot program

And you will be able to:

  • Use automated tools to assist with your porting effort
  • Fill in the gaps left by those automated tools

This training course is for you because...

  • You are an engineer working on a Python 2 application, and have been tasked with moving to Python 3
  • You are a team lead with a Python 2 project, and want to learn what’s involved with an eventual porting effort so you can help coordinate it

Prerequisites

Required background

  • Knowledge of Python 2, at least intermediate level

Class preparation

  • Install both Python 2 and Python 3

Recommended Preparation

Reading:

Exercises:

  • Run some sample code (TBD) under Python 3, and try to solve the problems that come up
  • Watch The Situation section from Porting from Python 2 to Python 3 video course

About your instructor

  • Ben Straub spent two years writing Python 3, and knows you’ll be happier with it than you are with Python 2. He’s been a software engineer for 13 years, and his experience runs includes working on device drivers, cross-platform utility libraries, source-control systems, desktop applications, API backends, and browser-based front-ends. He’s the creator of the Porting from Python 2 to Python 3 O’Reilly video series, as well as author of Pro Git, the canonical book on the Git source control tool.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Introduction and situational awareness (20 min)

  • The Situation
  • Porting vs. Polyglot
  • Automated tools

Exercise (10 min)

  • Run 2to3 and Futurize on a small code sample as a class

Python language changes (30 min presentation)

  • Numbers
  • Exceptions
  • Classes
  • Print
  • Repr
  • Imports & reload
  • Dicts
  • Ranges
  • Functional operators

Break (10 min)

Unicode and IO (30 min)

Break (10 min)

Exercise (15 min)
- Run 2to3 and Futurize on same code sample as a class

Q&A (5-15 min)

Break (10 min)

Library updates (45 min)

  • Collections
  • DBM modules
  • Commands and subprocess
  • Web libraries
  • IterTools
  • etc.

Exercise (30 min)

  • Translate a larger program from Python 2 to 3 as a class

Q&A (15 min)