sources/tech/20191120 Switching from Python 2 to Python 3- What you need to know.md
7.1 KiB
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.
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.
via: https://opensource.com/article/19/11/end-of-life-python-2
作者:Katie McLaughlin 选题:lujun9972 译者:译者ID 校对:校对者ID