【linux/windows】python2代码转python3-工具2to3
尽管现在有很多项目基于python2开发,但越来越多的项目以python3开发,而且越来越多的包开始不支持python2,就拿ubuntu系列版本来说,ubuntu20.04源库没有python-pip,或者做web开发的django框架3.0来说,直接python3,但项目肯定不可能直接重写,幸运的是不管win系统还是linux下都提供了对应的版本转换工具,只需简单执行相关命令,即可完成python2转python3。
1.windows下转换
在我们安装的python文件目录下,最好在官网下载zip包,解压后放置任何位置,同时设置环境变量,保证在命令行下运行python可以运行,当然嫌麻烦也可以下载那种一路next的版本,环境变量默认帮你设置好的。
假定上述工作你已完成的情况下,接下来打开python的安装目录,比如以下是我自己的目录及文件:
注意2to3.py
,通过此文件就可以完成版本转换,下面是使用实例。
# 查看哪些是需要更改的
python 2to3.py test.py
# py2转py3
python 2to3.py -w test.py
# 批量转换或查看更改部分
python 2to3.py [-w] ./
2.linux下的转换-ubuntu18.04
在ubuntu下,直接有这样一个二进制的命令2to3
,可以直接运行这个命令更改相关代码。
# 查看命令
root@ubuntu:~# ls -l /usr/bin/ | grep 2to3
-rwxr-xr-x 1 root root 96 Feb 27 23:10 2to3-2.7
# 查看帮助
root@ubuntu:~# 2to3-2.7 --help
Usage: 2to3 [options] file|dir ...
Options:
-h, --help show this help message and exit
-d, --doctests_only Fix up doctests only
-f FIX, --fix=FIX Each FIX specifies a transformation; default: all
-j PROCESSES, --processes=PROCESSES
Run 2to3 concurrently
-x NOFIX, --nofix=NOFIX
Prevent a transformation from being run
-l, --list-fixes List available transformations
-p, --print-function Modify the grammar so that print() is a function
-v, --verbose More verbose logging
--no-diffs Don't show diffs of the refactoring
-w, --write Write back modified files # 使用后,后将文件转换python3,同时多出后缀.bak的备份文件,python2版本
-n, --nobackups Don't write backups for modified files
-o OUTPUT_DIR, --output-dir=OUTPUT_DIR
Put output files in this directory instead of
overwriting the input files. Requires -n.
-W, --write-unchanged-files
Also write files even if no changes were required
(useful with --output-dir); implies -w.
--add-suffix=ADD_SUFFIX
Append this string to all output filenames. Requires
-n if non-empty. ex: --add-suffix='3' will generate
.py3 files.
# 查看需要更改处
root@ubuntu:~/test2to3# 2to3-2.7 pkg/p_demo.py.bak
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored pkg/p_demo.py.bak
--- pkg/p_demo.py.bak (original)
+++ pkg/p_demo.py.bak (refactored)
@@ -1,2 +1,2 @@
#!/usr/bin/python
-print "p_demo file"
+print("p_demo file")
RefactoringTool: Files that need to be modified:
RefactoringTool: pkg/p_demo.py.bak
# py2 -> py3
root@ubuntu:~/test2to3# 2to3-2.7 pkg/p_demo.py.bak -w
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored pkg/p_demo.py.bak
--- pkg/p_demo.py.bak (original)
+++ pkg/p_demo.py.bak (refactored)
@@ -1,2 +1,2 @@
#!/usr/bin/python
-print "p_demo file"
+print("p_demo file")
RefactoringTool: Files that were modified:
RefactoringTool: pkg/p_demo.py.bak
# 也可以直接对目录进行操作
root@ubuntu:~/test2to3# 2to3-2.7 [-w] ./
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: No changes to ./pkg/p_demo.py
RefactoringTool: No changes to ./proj/pro_demo2.py
RefactoringTool: No changes to ./tools/t_demo3.py
RefactoringTool: Files that need to be modified:
RefactoringTool: ./pkg/p_demo.py
RefactoringTool: ./proj/pro_demo2.py
RefactoringTool: ./tools/t_demo3.py
以上是不同系统下使用2to3工具转换python版本,希望对你有用。