转换Python2 -> Python3
藉由python35\Tools\scripts\2to3.py 档转换
python D:\python\Tools\scripts\2to3.py D:\Users\a0979\Desktop\scrip.py
python [转档程式] [欲转档.py]
* python 为 python3 ->因为转档程式为python3
![](https://i.imgur.com/PeQkvhk.png)
python2-scrip2.py
import httplib
i = 99999999L
print type(i)
try:
print 5/2
except ValueError, e:
print "Value Error Detected! "
j = 10
print j
print [j for j in range(15)]
print j
python3-scrip.py
import http.client
i = 99999999
print(type(i))
try:
print(5/2)
except ValueError as e:
print("Value Error Detected! ")
j = 10
print (j)
print ([j for j in range(15)])
print (j)
差别
-
语法差异
-
import 函式库上httplib 在python3中被取代为http.client
-
print 上python3要求使用者将参数加上()号
-
try-except 将原本的,改为as
-
-
语意差异
- python3 无long长整数->int
i = 99999999L
print type(i)
#python2 -> <type 'long'>
#python3 -> <class 'int'>
2.python3 自动化为浮点数
print 5/2
#python2 -> 2
#python3 -> 2.5
Linux中Python2.x 转换Python3.x
-
使用2to3指令
2to3 [python2专案]
下载Python3
wget https://www.python.org/ftp/python/3.2/Python-3.2.tar.bz2
tar -xvjf Python-3.2.tar.bz2 -C /opt #解壓縮到 /opt
cd /opt/Python-3.2
./configure
make
make install
使用2to3转档
2to3 -w script.py
#会产生一个script.py -> Python3 script.py.bak -> Python2
- 执行程式码
cp script.py.bak script2.py
python script2.py
python3 script.py
结果与windows相同