36 lines
2.4 KiB
Markdown
36 lines
2.4 KiB
Markdown
|
|
# 从 Python 2 迁移到 3
|
|||
|
|
|
|||
|
|
> 原文:[https://python.land/migrating-from-python-2-to-3](https://python.land/migrating-from-python-2-to-3)
|
|||
|
|
|
|||
|
|
目录
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
* 为什么要迁移?
|
|||
|
|
* [可以同时拥有 python 2 和 3 吗?](#Can_you_have_python_2_and_3_at_the_same_time "Can you have python 2 and 3 at the same time?")
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
## 为什么要迁移?
|
|||
|
|
|
|||
|
|
因为已经不支持 Python 2 了!对 Python 2 的支持应该在 2020 年初就停止了。上一个主要的 2.7.x 版本是在 2020 年 4 月。Python 2 的所有开发都已停止。这意味着不会有安全更新。
|
|||
|
|
|
|||
|
|
许多包维护者已经迁移到 Python 3。一些仍然支持 Python 2,而另一些已经放弃了支持。从这里开始,大部分包会逐渐停止支持。Python 3.0 于 2008 年 12 月 3 日发布。所以是的,我们都有足够的时间迁移。如果你还没有,你现在应该把它作为头等大事。最多,你应该在 2020 年底之前运行 Python 3。否则,您将面临漏洞、无法运行的软件等风险。
|
|||
|
|
|
|||
|
|
除了安全性和支持,Python 3 还有很多好处;这种语言已经变得更好了,这可以在我们关于 Python 3 的优势的页面上看到。
|
|||
|
|
|
|||
|
|
## 可以同时拥有 python 2 和 3 吗?
|
|||
|
|
|
|||
|
|
如果您真的需要为遗留代码库运行 Python 2,您可以轻松地将它与 Python 3 一起运行。
|
|||
|
|
|
|||
|
|
Thank you for reading my tutorials. I write these in my free time, and it requires a lot of time and effort. I use ads to keep writing these *free* articles, I hope you understand! **Support me by disabling your adblocker on my website** or, alternatively, **[buy me some coffee](https://www.buymeacoffee.com/pythonland)**. It's much appreciated and allows me to keep working on this site!
|
|||
|
|
|
|||
|
|
### Windows 操作系统
|
|||
|
|
|
|||
|
|
要在 Windows 上同时运行 Python 2 和 3,你需要[安装两个版本的 Python](https://python.land/installing-python) 。之后,我推荐使用 [Python 启动器](https://www.python.org/dev/peps/pep-0397/)。这个启动器可以用`py`命令调用。它将检查您的脚本中所谓的指定 Python 2 或 Python 3 的 shebang。
|
|||
|
|
|
|||
|
|
或者,您可以明确地选择一个版本:使用`py -3 yourscript.py`用 Python 3 启动您的脚本,或者使用`py -2 yourscript.py`用 Python 2 启动它。
|
|||
|
|
|
|||
|
|
### Linux 操作系统
|
|||
|
|
|
|||
|
|
如果你想在 Linux 上同时安装 Python 2 和 Python 3,你需要同时安装两者。Python 2 通常可以用`python`命令启动,而 Python 3 则用`python3`命令启动。
|