Python2迁徙Python3
有一些程序还是用Python2完成的,没有升级到Python3,因此在不得不使用时就需要改很多部分,针对走过的坑做记录
print
第一个就是打印输出,Python2中无括号,Python3中必须有No module named 'urllib2'
,Python3中无urllib2
,具体可见注解[1],修改方法是用urllib.request
代替urllib2
即:import urllib.request
ImportError: cannot import name 'izip'
,Python3里的zip就相当于python2 itertools里的izip。把报错的from itertools import izip注释掉,下面加一行izip = zip
试试ImportError: No module named 'cPickle'
Python3中这个模块改名了,所以将import cPickleas pickle
改为import pickle as pickle
注解:
[1]:官方对于urllib2
的说明
Note The urllib2 module has been split across several modules in
Python 3.0 named urllib.request and urllib.error. The 2to3 tool will
automatically adapt imports when converting your sources to 3.0
from urllib.request
import urlopen
response = urlopen(“http://www.google.com”)
html = response.read()
print(html)