Switching from Python 2 to Python 3: What you need to know

Python 2 will reach its end of life in mere weeks. Here's what to know before you migrate to Python 3.
221 readers like this.
A sunrise

Opensource.com

Python 2.7 will officially become unsupported beginning January 1, 2020. There is one final bugfix planned after this date, but then that's it.

What does this end of life (EOL) mean for you? If you run Python 2, you need to migrate.

Who decided to EOL Python 2?

In 2012, the team maintaining the Python programming language reviewed its options. There were two increasingly different codebases, Python 2 and Python 3. Both were popular, but the newer version was not as widely adopted.

In addition to Python 3's disruption of changing the underlying way data is handled by completely reworking Unicode support, a major version change allowed non-backward-compatible changes to happen all at once. This decision was documented in 2006. To ease the disruption, Python 2 continued to be maintained, with some features backported. To further help the community transition, the EOL date was extended from 2015 to 2020, another five years.

Maintaining divergent codebases was a hassle the team knew it had to resolve. Ultimately, a decision was announced:

"We are volunteers who make and take care of the Python programming language. We have decided that January 1, 2020, will be the day that we sunset Python 2. That means that we will not improve it anymore after that day, even if someone finds a security problem in it. You should upgrade to Python 3 as soon as you can."

Nick Coghlan, a core CPython developer and current member of the Python steering council, adds more information in his blog. And PEP 404, written by Barry Warsaw (also a member of the Python steering council), details why Python 2.8 will never be a thing.

Is anyone still supporting Python 2?

Support for Python 2 from providers and vendors will vary. Google Cloud has announced how it plans to support Python 2 going forward. Red Hat has also announced plans for Red Hat Enterprise Linux (RHEL), and AWS has announced minor version update requirements for the AWS command-line interface and SDK.

You can also read the Stack Overflow blog post "Why is the Migration to Python 3 Taking So Long?" by Vicki Boykis, in which she identifies three reasons why Python 3 adoption is slow. 

Reasons to use Python 3

Regardless of ongoing support, it's a really good idea to migrate to Python 3 as soon as you can. Python 3 will continue to be supported, and it has some really neat things that Python 2 just doesn't have.

The recently released Python 3.8 includes such features as the walrus operator, positional-only parameters, and self-documenting f-strings. Earlier releases of Python 3 introduced features such as asyncio, f-strings, type hints, and pathlib, just to name a few.

The top 360 most-downloaded packages have already migrated to Python 3. You can check your requirements.txt file using the caniusepython3 package to see if any packages you depend on haven't yet been migrated.

Resources for porting Python 2 to Python 3

There are many resources available to ease your migration to Python 3. For example, the Porting Python 2 to Python 3 guide lists a bunch of tools and tricks to help you achieve single-source Python 2/3 compatibility. There are also some useful tips on Python3statement.org.

Dustin Ingram and Chris Wilcox gave a presentation at Cloud Next '19 detailing some of the motivations and migration patterns for the transition into Python 3. Trey Hunner gave a presentation at PyCon 2018 about Python 3's most useful features to encourage you to migrate so you can take advantage of them.

Join us!

January 1, 2020, is now just weeks away. If you need daily reminders of just how soon that is (and you use Twitter), follow the Countdown to Python 2 sunset Twitter bot.

What to read next
Tags
User profile image.
Katie has worn many different hats over the years. She has previously been a software developer for many languages, systems administrator for multiple operating systems, and speaker on many different topics. When she's not changing the world, she enjoys making cooking, tapestries, and seeing just how well various application stacks handle emoji.

5 Comments

One thing I'm trying to figure out is how datetime works in Python 3. I have a script that contains the following in Python2:

today = date.today()
d = today.strftime("%A, %B %d, %Y")

What d ends up is today's date, in the form of Day of Week, Month, Date, and Year.
This doesn't work in Python3, but on searching around, I still can't figure out how to do this in Python3.

For users of Scribus, the svn version of Scribus now requires Python3.

The following seems to work in Python 3.6:

$ python3
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> today = datetime.date.today()
>>> today.strftime("%A, %B %d, %Y")
'Wednesday, November 20, 2019'
>>>

In reply to by Greg P

Great! Thank you

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.