pip uninstall python2

文章目录


熟悉python的人都知道python2和python3的语法和库有部分是不兼容的,尽管在2017年python官方就已经公布消息会停止对python2的官方支持,但是由于工业上python2当时用的比较多,这个时间就又被延长了,而在2018年,python之父Guido van Rossum 也在邮件里回复了何时终结python2.7的支持

邮件内容是这样的:

Let’s not play games with semantics. The way I see the situation for 2.7 is that EOL is January 1st, 2020, and there will be no updates, not even source-only security patches, after that date. Support (from the core devs, the PSF, and python.org) stops completely on that date. If you want support for 2.7 beyond that day you will have to pay a commercial vendor. Of course it’s open source so people are also welcome to fork it. But the core devs have toiled long enough, and the 2020 EOL date (an extension from the originally annouced 2015 EOL!) was announced with sufficient lead time and fanfare that I don’t feel bad about stopping to support it at all.

大意上就是说:

Python 2.7 的 EOL 日期是 2020 年 1 月 1 日,之后不会有任何更新,源码的安全补丁也不会有了。

2020 年元旦之后,来自 Python 核心开发团队、PSF(Python 软件基金会)、Python.org 的支持,统统都停掉。

如果以后你想要对 2.7 的支持,你得给商业供应商掏钱了。当然了,因为 Python 是开源的,所以也欢迎大家来 fork。

不过不用慌,从 Python 2 迁移到 Python 3 却并没有想象中那么难。为了更方便把python2迁移到python3,python3中自带了2to3工具。​

安装 2to3

打开pycharm"setting">“Tools”>“External Tools”
在这里插入图片描述
name:2to3(可以自定义,自己知道什么功能就行)
Description:描述,自己看着办,可以不写
Program:自己电脑上python解释器的路径,这里附上我做参考

E:\tools\python\step\python.exe

Arguments:

E:\tools\python\step\Tools\scripts\2to3.py
-w
$FilePath$

2to3.py的路径要写自己的,路径可以参考,都是在Tools\scripts下
working :

$FileDir$

然后确定保存设置即可

使用2to3

下面我们试一下

print "hello"

上面是python2的语法
对其执行 2to3 脚本:

RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored E:\bigdata\prj\prj1\test_line.py
RefactoringTool: Files that were modified:
RefactoringTool: E:\bigdata\prj\prj1\test_line.py
--- E:\bigdata\prj\prj1\test_line.py	(original)
+++ E:\bigdata\prj\prj1\test_line.py	(refactored)
@@ -1 +1 @@
-print "hello"
+print("hello")

在默认情况下,2to3 只会对迁移到 Python 3 时必须作出修改的代码进行标示,在输出结果中显示的 Python 3 代码是直接可用的,但你可以在 2to3 加上 -w 或者 --write 参数,这样它就可以直接按照给出的方案修改你的 Python 2 代码文件了。

2to3 脚本不仅仅对单个文件有效,你还可以把它用于一个目录下的所有 Python 文件,同时它也会递归地对所有子目录下的 Python 文件都生效。

发布了21 篇原创文章 · 获赞 28 · 访问量 3714

猜你喜欢

转载自blog.csdn.net/LPJCSY/article/details/103804664
今日推荐