'Pickle can't find module that I am not using?

I have a script using these modules to generate a model.

import pandas as pd
import seaborn as sns
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import accuracy_score
from sklearn.ensemble import GradientBoostingClassifier
from imblearn.over_sampling import RandomOverSampler
from datetime import datetime
import pickle

Then when I unpickle the model, I get:

ImportError: No module named _gb

I am super confused, what the issue could be?

i have tried & I can manually import all of those modules in the Python shell

Thanks a lot

  File "/data/keenek1/production/ncx3_nps.py", line 232, in <module>
    model_det = pickle.load(file)
  File "/app/anaconda2/lib/python2.7/pickle.py", line 1384, in load
    return Unpickler(file).load()
  File "/app/anaconda2/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/app/anaconda2/lib/python2.7/pickle.py", line 1096, in load_global
    klass = self.find_class(module, name)
  File "/app/anaconda2/lib/python2.7/pickle.py", line 1130, in find_class
    __import__(module)
ImportError: No module named _gb


Solution 1:[1]

This happens when saving and loading occur in different environments.

I believe for Pickle to work correctly all modules related to the model have to have the same version when pickling and unpickling; python version and the operating system have to be the same.

No module named _gb error specifically can occur when a model saved in Windows is loaded in Linux environment.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ap31