ImportError: cannot import name 'joblib' from 'sklearn.externals'

Praneeth Sai picture Praneeth Sai · May 19, 2020 · Viewed 85.1k times · Source

I am trying to load my saved model from s3 using joblib

import pandas as pd 
import numpy as np
import json
import subprocess
import sqlalchemy
from sklearn.externals import joblib

ENV = 'dev'
model_d2v = load_d2v('model_d2v_version_002', ENV)

def load_d2v(fname, env):
    model_name = fname
    if env == 'dev':
        try: 
            model=joblib.load(model_name)
        except:
            s3_base_path='s3://sd-flikku/datalake/doc2vec_model'
            path = s3_base_path+'/'+model_name
            command = "aws s3 cp {} {}".format(path,model_name).split()
            print('loading...'+model_name)
            subprocess.call(command)
            model=joblib.load(model_name)
    else:
        s3_base_path='s3://sd-flikku/datalake/doc2vec_model'
        path = s3_base_path+'/'+model_name
        command = "aws s3 cp {} {}".format(path,model_name).split()
        print('loading...'+model_name)
        subprocess.call(command)
        model=joblib.load(model_name)
    return model

But i was facing this error:

    from sklearn.externals import joblib
ImportError: cannot import name 'joblib' from 'sklearn.externals' (C:\Users\prane\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\externals\__init__.py)

Then i tried installing joblib directly by doing

import joblib

but it gave me this error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in load_d2v_from_s3
  File "/home/ec2-user/.local/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 585, in load
    obj = _unpickle(fobj, filename, mmap_mode)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 504, in _unpickle
    obj = unpickler.load()
  File "/usr/lib64/python3.7/pickle.py", line 1088, in load
    dispatch[key[0]](self)
  File "/usr/lib64/python3.7/pickle.py", line 1376, in load_global
    klass = self.find_class(module, name)
  File "/usr/lib64/python3.7/pickle.py", line 1426, in find_class
    __import__(module, level=0)
ModuleNotFoundError: No module named 'sklearn.externals.joblib'

Can you tell me how to solve this? Thanks in advance

Answer

Smruthi Vuppuluri picture Smruthi Vuppuluri · Jul 8, 2020

You can directly use

import joblib

instead of

from sklearn.externals import joblib.

I tried this out and it worked well for me