Python read pickle protocol 4 error: STACK_GLOBAL requires str
Asked Answered
C

6

8

In Python 3.7.5, ubuntu 18.04, pickle read gives error,

pickle version 4

Sample code:

import pickle as pkl
file = open("sample.pkl", "rb")
data = pkl.load(file)

Error:

UnpicklingError Traceback (most recent call last) in
----> 1 data = pickle.load(file) UnpicklingError: STACK_GLOBAL requires str

Reading from same file object solves problem.

Reading using pandas also gives same problem

Cray answered 20/11, 2019 at 8:23 Comment(3)
Elbek has correctly pointed out that the error message that they received can be related to a known issue. However, the same error message can also occur when one tries to load a file that is simply not a pickle file. For example, if (as it happened to me today), one accidentally tries to load a file that contains a numpy array via pickle.load one receives the same error message as Elbek.Theaterintheround
I agree with @AliceSchwarze. The most probable explanation is that you load numpy array that was saved by using np.save.Subside
Numpy load/save functions rely on pickle by default.Undersurface
C
0

Turns out it is known issue. There is issue page in github

Cray answered 21/1, 2020 at 5:38 Comment(0)
G
17

I also has this error turned out I was opening a numpy file with pickle. ;)

Gait answered 3/3, 2021 at 12:43 Comment(2)
how did you solve it? can you paste the edited code? thanks!Knuth
I don't think you can open numpy files using pcikle. you need to use something like numpy.load() numpy.org/doc/stable/reference/generated/numpy.load.htmlGait
F
1

My problem was that I was trying to pickle and un-pickle across different python environments - watch out to make sure your pickle versions match!

Futility answered 8/12, 2021 at 17:29 Comment(0)
C
0

Turns out it is known issue. There is issue page in github

Cray answered 21/1, 2020 at 5:38 Comment(0)
F
0

I had this problem and just added pckl to the end of the file name.

Fukuoka answered 9/4, 2020 at 19:19 Comment(0)
C
0

Perhaps this will be the solution to this error for someone. I needed to load a numpy array:

torch.load(file)

When I loaded the array, this error appeared. All that is needed is to turn the array into a tensor. For example:

result = torch.from_numpy(np.load(file))
Commemorate answered 27/5, 2022 at 13:11 Comment(0)
K
0

If you are encountering problem in YOLO V4 or V7 model, Simply delete the labels.cache that is being generated. This may happen due to different configuration settings of various YOLO models. For me my dataset already had .cache file in it so removing it help me solve this problem.

Kat answered 16/3 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.