NameError: name 'reload' is not defined等python版本问题解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yql_617540298/article/details/82699765

        python2.x和python3.x之间相差很多,有很多语法知识已经不再适用于新的python版本,下面简单列出3个错误:

一、pickle.load错误:a bytes-like object is required, not 'str'

pickle(除了最早的版本外)是二进制格式的,所以应该带 'b' 标志打开文件;

f=open('./a.txt','rb')
label_dict = pickle.load(f)

二、TypeError: slice indices must be integers or None or have an __index__ method

由于除法/自动产生的类型是浮点型,因此出现上述错误,修正方法为,将/更改为//;

start_width = (width_large - width_small) // 2
start_height = (height_large - height_small) // 2

三、 NameError: name 'reload' is not defined

python版本导致:

(1)对于 Python 2.X

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

(2)对于 <= Python 3.3

import imp
imp.reload(sys)

(3)对于>= Python 3.4

import sys
import importlib
importlib.reload(sys)

猜你喜欢

转载自blog.csdn.net/yql_617540298/article/details/82699765