pickle报错 ValueError: unsupported pickle protocol: 3

简介: pickle报错 ValueError: unsupported pickle protocol: 3

pickle报错 ValueError: unsupported pickle protocol: 3


1、报错含义

不支持用 Python2 读取 Python3 生成的 pkl 文件。

2、解决办法

如果知道会是在 Python2 环境中执行读取 pkl 文件,则在生成 pkl 文件时加入 protocol=2:

import pickle
pickle.dump(model_name,open('example.pkl', 'wb'), protocol=2)

这样就是用 Python3 生成了能用 Python2 读取的 pkl 文件。

3、补充

Python2 生成的 pkl 导入方式:

import cPickle

with open('data.pkl', 'rb') as f:

samples, labels = cPickle.load(f)

Python3 生成的pkl 导入方式:

import pickle

with open('data.pkl', 'rb') as f:

samples, labels = pickle.load(f)

相关文章
|
6月前
|
开发者 Python
【Python】已解决:TypeError: __init__() got an unexpected keyword argument ‘port’
【Python】已解决:TypeError: __init__() got an unexpected keyword argument ‘port’
937 0
【Python】已解决:TypeError: __init__() got an unexpected keyword argument ‘port’
成功解决ForkingPickler(file, protocol).dump(obj) TypeError: can't pickle Environment objects
成功解决ForkingPickler(file, protocol).dump(obj) TypeError: can't pickle Environment objects
成功解决ForkingPickler(file, protocol).dump(obj) TypeError: can't pickle Environment objects
|
4月前
|
JSON 安全 数据格式
7-6|python报错TypeError: can't pickle _thread.RLock objects
7-6|python报错TypeError: can't pickle _thread.RLock objects
|
6月前
|
数据处理 Python
【Python】已解决:raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+‘; not supported’) xlrd.biffh.XLRD
【Python】已解决:raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+‘; not supported’) xlrd.biffh.XLRD
138 1
|
6月前
|
数据挖掘 数据处理 Python
【Python】已解决:TypeError: read_csv() got an unexpected keyword argument ‘shkiprows‘
【Python】已解决:TypeError: read_csv() got an unexpected keyword argument ‘shkiprows‘
157 0
|
6月前
|
Python
【Python】已解决:TypeError: write._Log() takes 0 positional arguments but 1 was given
【Python】已解决:TypeError: write._Log() takes 0 positional arguments but 1 was given
243 0
|
6月前
|
Python
【Python】已解决:TypeError: *init*() missing 1 required positional argument: ‘scheme’
【Python】已解决:TypeError: *init*() missing 1 required positional argument: ‘scheme’
415 0
|
8月前
|
Linux Windows Python
ForkingPickler(file, protocol).dump(obj) TypeError: can‘t pickle Environment objects
ForkingPickler(file, protocol).dump(obj) TypeError: can‘t pickle Environment objects
172 0
|
异构计算
解决ValueError: Unsupported ONNX opset version: 16问题
解决ValueError: Unsupported ONNX opset version: 16问题
803 0
_pickle.UnpicklingError: invalid load key, ‘H‘
_pickle.UnpicklingError: invalid load key, ‘H‘