Cannot load pickle file when containing instance of custom class
See original GitHub issueI cannot load a pickle file when containing instance of custom class. The error message is: ModuleNotFoundError: No module named ‘imgres’ (imgres is the module) A remark, the module (at least the class) is loaded because I can create an instance of this class (this works
I use
- Python 3.7.4
- Windows 10
I zipped everything into the attached file AZ12_FuncPickleProblem.zip
Essential folders and files in the zip
TerminalOutput.txt
contains the terminal output with the error message.other
: folder containing script to create pickle file and example of loading itimgres.py
contains my class ImgResdump_pickle_file.py
create pickle filemyimgres.pkl pickle
fileload_pickle_file.py
test loading pickle file myimgres.pkl (astonishingly there is no need for import of module, it only have to be present)
azurefunc
: folder of azure function projectmyimgres.pkl
pickle file to be readHttpTriggerTest
: folder with function named HttpTriggerTest__init__.py
source, error inmyimgresread = pickle.load(f)
imgres.py
module
What am I doing wrong? How can I solve this problem? It would be great if anybody could help!
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Unable to load files using pickle and multiple modules
The issue is that you're pickling objects defined in Settings by actually running the 'Settings' module, then you're trying to unpickle the ...
Read more >pickle — Python object serialization — Python 3.11.1 ...
Shared objects remain shared, which can be very important for mutable objects. marshal cannot be used to serialize user-defined classes and their instances....
Read more >Using the cPickle Module on Classes and Instances - O'Reilly
This causes a TypeError : “can't pickle file objects exception”, because the state of anotherInstance includes a file object, and file objects cannot...
Read more >The ultimate guide to Python pickle - Snyk
This article will teach you how to safely use Python's built-in pickle library to maintain persistence within complex data structures.
Read more >11.1. pickle — Python object serialization - 3.7.9 Documentation
marshal cannot be used to serialize user-defined classes and their instances. pickle can save and restore class instances transparently, however the class ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @HoLue,
Sorry for making you wait so long, please see the following code that should work in your scenario.
Basically, the issue you’re facing is not Azure Functions specific. The pickle serializer will also record the module import path in the .pkl file. The reason why it keeps throwing Module Not Found error during unpickling is because the
imgres
is never registered in the system modules.There’s a comprehensive solution for this kind of issue from the stack overflow. Unpickling python objects with a changed module path
If there’s any more question, feel free to reopen the thread. Thanks for your patience.
@Hazhzeng are you a god coming from heaven?