Import error: No module named "utils" when using pickle.load
Asked Answered
F

1

6

I firstly dump some stuff into a pickle file using pickle.dump. in utils.load_data, my project hierarchy looks like this

project1
  -utils
     -__init__.py
     -load_data.py
  -data
  (other folder...)

Then it outputs a pickle file into a data folder. Then I move the .pickle file to another project, the project hierarchy is

project2
   -data
   -main.py

When I run a pickle.load() operation in this main.py, it prompts the error as the title. However, if I move main.py back to project1 folder, then the error disappears. So the issue must be from the file.

My question is, why does pickle try to import the package from where it was born? Could anyone share a good explanation for this? I got quite confused.

Fanlight answered 26/7, 2020 at 14:39 Comment(0)
S
10

By default, unpickling will import any class that it finds in the pickle data. This means if you have pickled a custom class and you are unpickling it somewhere, pickle will try import the module (utils in this case). So you need to have the utils module inside project2 folder

Follow this for more information

Sweptback answered 26/7, 2020 at 18:2 Comment(3)
You can accept the answer if it was useful. It helps others discover the answer.Sweptback
This is the best answer I can find on this topic!Serranid
Many Thanks for your anwser, it helped me a lot, after huge exploration on stackoverflow !Undertaker

© 2022 - 2024 — McMap. All rights reserved.