Solve – Unable to load files using pickle and multiple modules in Python

Solve – Unable to load files using pickle and multiple modules in Python is an error which occurs when you are working with pickle and objects in Python.

In this article we are going to explain why the error Unable to load files using pickle occurs and I will show you how to solve the error and get rid of it for good using multiple solutions.

Explaining the Error : Unable to load files using pickle and multiple modules in Python

The error occurs in a lot of possible scenarios and it is hard to try to reproduce the error as there are a lot of reasons why this could happens.

The individual/particular scenario does not matter as long as the error you are getting looks a little bit like the error below.

                                                                       #
Traceback (most recent call last):
  File "C:\Users`Program\LoginGUI.py", line 53, in <module>
    Settings.loadUsers(users)
  File "C:\Users\Program\Settings.py", line 51, in loadUsers
    temp = pickle.load(file)
AttributeError: Can't get attribute 'Manager' on <module '__main__' (built-in)>
                                                                       #

To solve the problem above, I have a couple of solutions which worked for me, bellow is a detailed explanation of them.

Solution 1 : Using pickle.Unpickler

One of the tricks one can try to solve this error quickly is to use the pickle.Unpickler class in python3.

You can override the find_class method by using the aforementioned class.

In order to do that you can follow the example bellow.

                                                                       #
import pickle
class CustomUnpickler(pickle.Unpickler):
    def find_class(self, module, name):
        if name == 'Ticker':
            from settings import Ticker
            return Ticker
        return super().find_class(module, name)
pickle_data = CustomUnpickler(open('file_path.pkl', 'rb')).load()
                                                                       #

If the solution above does not solve the problem, please try the solutions bellow.

Solution 2 : importing the classes that are outside the module

This is a Solution for people who have classes that are outside the module.

The trick is to import the classes, here is a base example that can help you.

But first, do not forget to create an __init__.py file for the module folder.

                                                                       #
from outside_module import DefinedClass1, DefinedClass2, DefinedClass3 
with open('pickle_file.pkl', 'rb') as f:
pickle_data = pickle.load(f)
                                                                       #

I hope this solution helped solved the issue for you

Good luck with your Python journey and with the scripts to come.

Summing-up

Guys, this has been my best attempt at helping you understand and solve this issue. I hope you found a solution which suits your needs.

Consider helping the blog if you can by donating to our Kofi account. Thank you for reading, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/