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.
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