从 Python 2 切换到 Python 3 你所需要了解的
Python

> 
> Python 2 将在几周内走到生命终点，这篇文章是你迁移到 Python 3 之前应该知道的。
> 
> 
> 


![](/data/attachment/album/201912/01/095336lbppn8qp1nnnwqqp.jpg)


从 2020 年 1 月 1 日开始，Python 2.7 将不再得到正式支持。在此日期之后，将会发布一个[最终错误修复](https://www.python.org/dev/peps/pep-0373/#maintenance-releases)计划，但是仅此而已。


Python 2 的生命终点（EOL）对你意味着什么？如果正在运行着 Python 2，你需要迁移。


### 是谁决定 Python 2 的生命终点？


在 [2012](https://github.com/python/peps/commit/a733bc927acbca16bfa3de486fb2c7d3f767a748) 年，维护 Python 编程语言的团队审查了其选项。有两个越来越不同的代码库，Python 2 和 Python 3。这两者都很流行，但是较新的版本并未得到广泛采用。


除了 Python 3 中完全重写的 Unicode 支持改变了处理数据的底层方式造成的断层，这个主要版本的变化还一次性出现了一些非向后兼容的更改。这种断层的决定成文于 [2006 年](https://www.python.org/dev/peps/pep-3000/#compatibility-and-transition)。为了减轻该断层的影响，Python 2 继续保持了维护，并向后移植了一些 Python 3 的功能。为了进一步帮助社区过渡，EOL 日期[从 2015 年延长至 2020 年](https://github.com/python/peps/commit/f82462002b86feff36215b4230be28967039b0cc)，又延长了五年。


该团队知道，维护不同的代码库是必须解决的麻烦。最终，他们[宣布了](https://www.python.org/doc/sunset-python-2/)一项决定：



> 
> “我们是制作和照料 Python 编程语言的志愿者。我们已决定 2020 年 1 月 1 日将是我们停止使用 Python 2 的日子。这意味着在这一天之后，即使有人发现其中存在安全问题，我们也将不再对其进行改进。你应尽快升级到 Python 3。”
> 
> 
> 


[Nick Coghlan](https://twitter.com/ncoghlan_dev) 是 CPython 的核心开发人员，也是 Python 指导委员会的现任成员，[在他的博客中添加了更多信息](http://python-notes.curiousefficiency.org/en/latest/python3/questions_and_answers.html)。由 [Barry Warsaw](https://twitter.com/pumpichank)（也是 Python 指导委员会的成员）撰写的 [PEP 404](https://www.python.org/dev/peps/pep-0404/) 详细说明了 Python 2.8 永远不会面世的原因。


### 有人还在支持 Python 2 吗？


提供者和供应商对 Python 2 的支持会有所不同。[Google Cloud 宣布了](https://cloud.google.com/python/docs/python2-sunset/?utm_source=osdc&utm_medium=blog&utm_campaign=pysunset)它计划未来如何支持 Python 2。红帽还[宣布了红帽企业 Linux（RHEL）的计划](https://access.redhat.com/solutions/4455511)，而 AWS 宣布了 AWS 命令行界面和 [SDK](https://aws.amazon.com/sdk-for-python/) 的[次要版本更新要求](https://aws.amazon.com/blogs/developer/deprecation-of-python-2-6-and-python-3-3-in-botocore-boto3-and-the-aws-cli/)。


你还可以阅读 [Vicki Boykis](https://twitter.com/vboykis) 在 Stack Overflow 撰写的博客文章“[为什么迁移到 Python 3 需要这么长时间？](https://stackoverflow.blog/2019/11/14/why-is-the-migration-to-python-3-taking-so-long/)”，其中她指出了采用 Python 3 缓慢的三个原因。


### 使用 Python 3 的原因


不管是否有持续的支持，尽快迁移到 Python 3 是一个好主意。Python 3 将继续受到支持，它具有 Python 2 所没有的一些非常优雅的东西。


最近发布的 [Python 3.8](https://www.python.org/downloads/) 包含 [海象运算符](https://docs.python.org/3.8/whatsnew/3.8.html#assignment-expressions)、[位置参数](https://docs.python.org/3.8/whatsnew/3.8.html#positional-only-parameters)和[自描述的格式化字符串](https://docs.python.org/3.8/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging)等[功能](https://docs.python.org/3.8/whatsnew/3.8.html)。Python 3 的早期版本引入的[功能](https://docs.python.org/3.8/whatsnew/index.html)，例如 [异步 IO](https://docs.python.org/3.8/library/asyncio.html#module-asyncio)、[格式化字符串](https://docs.python.org/3.7/whatsnew/3.6.html#pep-498-formatted-string-literals)、[类型提示](https://docs.python.org/3.7/whatsnew/3.5.html#pep-484-type-hints) 和 [pathlib](https://docs.python.org/3.8/library/pathlib.html#module-pathlib)，这里只提及了一点点。


下载最多的前 360 个软件包[已迁移到 Python 3](http://py3readiness.org/)。你可以使用 [caniusepython3](https://pypi.org/project/caniusepython3/) 软件包检查你的 `requirements.txt` 文件，以查看你依赖的任何软件包是否尚未迁移。


### 将 Python 2 移植到 Python 3 的参考资源


有许多参考资源可简化你向 Python 3 的迁移。例如，“[将 Python 2 移植到 Python 3 指南](https://docs.python.org/3/howto/pyporting.html)”列出了许多工具和技巧，可帮助你实现与 Python 2/3 单一源代码的兼容性。在 [Python3statement.org](https://python3statement.org/practicalities/) 上也有一些有用的技巧。


[Dustin Ingram](https://twitter.com/di_codes) 和 [Chris Wilcox](https://twitter.com/chriswilcox47) 在 [Cloud Next ‘19](https://www.youtube.com/watch?v=Bye7Rms0Vgw&utm_source=osdc&utm_medium=blog&utm_campaign=pysunset)上作了一个演讲，详细介绍了向 Python 3 过渡的一些动机和迁移模式。[Trey Hunner](https://twitter.com/treyhunner) 在 [PyCon 2018 演讲](https://www.youtube.com/watch?v=klaGx9Q_SOA)上介绍了 Python 3 最有用的功能，鼓励你进行迁移，以便你可以利用它们。


### 加入我们！


距 2020 年 1 月 1 日仅有几周了。如果你需要每天提醒一下它即将到来的时间（并且你使用 Twitter 的话），请关注 [Python 2 日落倒计时](https://twitter.com/python2sunset) Twitter 机器人。




---


via: <https://opensource.com/article/19/11/end-of-life-python-2>


作者：[Katie McLaughlin](https://opensource.com/users/glasnt) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Python 2.7 will officially become unsupported beginning January 1, 2020. There is one [final bugfix](https://www.python.org/dev/peps/pep-0373/#maintenance-releases) 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](https://github.com/python/peps/commit/a733bc927acbca16bfa3de486fb2c7d3f767a748), 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](https://www.python.org/dev/peps/pep-3000/#compatibility-and-transition). 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](https://github.com/python/peps/commit/f82462002b86feff36215b4230be28967039b0cc), another five years.

Maintaining divergent codebases was a hassle the team knew it had to resolve. Ultimately, a decision was [announced](https://www.python.org/doc/sunset-python-2/):

"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](https://twitter.com/ncoghlan_dev), a core CPython developer and current member of the Python steering council, [adds more information in his blog](http://python-notes.curiousefficiency.org/en/latest/python3/questions_and_answers.html). And [PEP 404](https://www.python.org/dev/peps/pep-0404/), written by [Barry Warsaw](https://twitter.com/pumpichank) (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](https://cloud.google.com/python/docs/python2-sunset/?utm_source=osdc&utm_medium=blog&utm_campaign=pysunset) how it plans to support Python 2 going forward. Red Hat has also [announced plans for Red Hat Enterprise Linux (RHEL)](https://access.redhat.com/solutions/4455511), and AWS has announced [minor version update requirements](https://aws.amazon.com/blogs/developer/deprecation-of-python-2-6-and-python-3-3-in-botocore-boto3-and-the-aws-cli/) for the AWS command-line interface and [SDK](https://aws.amazon.com/sdk-for-python/).

You can also read the Stack Overflow blog post "[Why is the Migration to Python 3 Taking So Long?](https://stackoverflow.blog/2019/11/14/why-is-the-migration-to-python-3-taking-so-long/)" by [Vicki Boykis](https://twitter.com/vboykis), 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](https://www.python.org/downloads/) includes such [features](https://docs.python.org/3.8/whatsnew/3.8.html) as the [walrus operator](https://docs.python.org/3.8/whatsnew/3.8.html#assignment-expressions), [positional-only parameters](https://docs.python.org/3.8/whatsnew/3.8.html#positional-only-parameters), and [self-documenting f-strings](https://docs.python.org/3.8/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging). Earlier releases of Python 3 introduced [features](https://docs.python.org/3.8/whatsnew/index.html) such as [asyncio](https://docs.python.org/3.8/library/asyncio.html#module-asyncio), [f-strings](https://docs.python.org/3.7/whatsnew/3.6.html#pep-498-formatted-string-literals), [type hints](https://docs.python.org/3.7/whatsnew/3.5.html#pep-484-type-hints), and [pathlib](https://docs.python.org/3.8/library/pathlib.html#module-pathlib), just to name a few.

The top 360 most-downloaded packages [have already migrated to Python 3](http://py3readiness.org/). You can check your requirements.txt file using the [caniusepython3](https://pypi.org/project/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](https://docs.python.org/3/howto/pyporting.html) 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](https://python3statement.org/practicalities/).

[Dustin Ingram](https://twitter.com/di_codes) and [Chris Wilcox](https://twitter.com/chriswilcox47) gave a [presentation at Cloud Next '19](https://www.youtube.com/watch?v=Bye7Rms0Vgw&utm_source=osdc&utm_medium=blog&utm_campaign=pysunset) detailing some of the motivations and migration patterns for the transition into Python 3. [Trey Hunner](https://twitter.com/treyhunner) gave a [presentation at PyCon 2018](https://www.youtube.com/watch?v=klaGx9Q_SOA) 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](https://twitter.com/python2sunset) Twitter bot.

## 5 Comments