Why You Must Migrate to Python 3 Now

You only have a few months left. Luckily, migrating is not that hard. Here are some helpful tips

Erik-Jan van Baaren
Mar 1 · 6 min read
Photo by Jonathan Farber on Unsplash.

It’s No Longer Supported

Support for Python 2 should have stopped at the beginning of 2020. However, it has become clear that the last major 2.7.x release will be in April 2020. After that, all development will cease for Python 2. This means there will be no security updates.

How to Check Which Version You’re Running

Just in case you don’t know or never cared, you can check your current Python version with the following command:

$ python --version
Python 2.7.16
python3 --version
Python 3.7.6

How to Upgrade/Install

I discovered this terrific site. It tells you exactly how to install Python 3 on Windows, Linux, macOS, and even Chromebooks. Go check it out and follow the guide there. I can’t do a better job!

How to Migrate Your Code

Now that you have Python 3 running, it’s time to upgrade your code. It’s not hard to upgrade your code. and you can follow multiple strategies. Most libraries are Python 3-compatible by now.

  1. if needed, upgrade to newer versions of dependencies that are Python 3 compatible


2to3 is a Python program that reads Python 2 source code and applies a series of fixers to transform it into valid Python 3 code. The standard library contains a rich set of fixers that will handle almost all code.

def greet(name):
print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
$ 2to3 greet.py
$ 2to3 -w greet.py
def greet(name):
print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
  • -x — excludes selected fixer
  • -f — explicitly run only this fix


six is a Python 2 and 3 compatibility library. The project helps codebases to support both Python 2 and 3. I would recommend migrating completely to Python 3 with 2to3, but if you can’t — for whatever reason — you can at least make your codebase work on both versions.

  • For a similar library, also check out the future package.

Upgrade your packages

You may need to upgrade the packages you depend on. For each package, try to find out if it already supports Python 3. If it doesn’t, find a version that does. You may have to alter some code since APIs tend to change over time.

Check for a minimum-required Python version

After you migrate your code, you can check for the Python version in your code. This way, you can ensure you and your users are not running your script with an incompatible version, which will cause incomprehensible errors and frustration. Use a simple check like this:

Some Advantages of Python 3

Just to whet your appetite a little more, here are a couple of advantages Python 3 has to offer.

Print is no longer a statement but a built-in function

Some of the advantages:

  • Since print is a function, it can be passed as an argument to functions that expect a function. Take for example a function that requires another function to further process your data as an argument. For simple mocking/debugging, you can now also pass the print() function.
  • You can use print like this—[print(x) for x in range(10)]—now because it is a function.
  • You can override the print function by assigning to builtins.print, whereas you can't do that with a statement.


In Python 3, every string is by default a Unicode string. In Python 2, a string defaults to an ASCII string, limiting the range of characters it can handle. If you wanted a Unicode string, you had to explicitly create one like this:

# no longer needed in Python 3
unicode_sting = u'Ümlaut? Nō prōblem!'

Data classes

Since version 3.7, which is fairly recent, Python offers data classes. There are several advantages over regular classes or other alternatives, like returning multiple values or dictionaries:

  • You can compare data classes because __eq__ is implemented for you.
  • You can easily print a data class for debugging because __repr__ is implemented as well.
  • Data classes require type hints, reducing the chances of bugs.

Merging dictionaries (Python 3.5+)

Since Python 3.5, it became easier to merge dictionaries:

Divisions became more predictable

In Python 2, the division operator ( / ) defaults to an integer division unless one of the operands is a floating-point number. So you have this behavior:

# Python 2
5 / 2 = 2
5 / 2.0 = 2.5
Python 3
5 / 2 = 2.5
5 // 2 = 2

Meaningful comparisons

In Python 2, you could compare anything to everything. The following example would all return True:

print "a string" > 2
print None < 5

No more range vs. xrange

Python 2 had two range functions: range and xrange. The latter was faster since it was based on iterators. In Python 3, range has become xrange and the xrange name was dropped. It’s one of the examples where Python became less confusing for learners.


I could keep raving about Python 3 vs. Python 2, but by now you should be convinced. The simple fact that support for Python 2 will be dropped somewhere after April 2020 should be enough for you to start converting those codebases!

Better Programming

Advice for programmers.

Thanks to Zack Shapiro

Erik-Jan van Baaren

Written by

A writer at heart and software/data engineer by profession. Subscribe to my low-volume newsletter at https://techexp.substack.com/

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Fun Side Projects That You Can Build Today


More from Better Programming

More from Better Programming

The Zero-Dollar Infrastructure Stack


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade