Tuesday, March 6, 2018

`ImportError: cannot import name _remove_dead_weakref` while importing from scikit learn

Leave a Comment

PROBLEM DESCRIPTION

I'm getting this error while calling a python script :

Traceback (most recent call last):   File "/path/t/file/file.py", line 61, in <module>     from sklearn.externals import joblib   File "/anaconda2/lib/python2.7/site-packages/sklearn/__init__.py", line 20, in <module>     import logging   File "/anaconda2/lib/python2.7/logging/__init__.py", line 26, in <module>     import sys, os, time, cStringIO, traceback, warnings, weakref, collections   File "/anaconda2/lib/python2.7/weakref.py", line 14, in <module>     from _weakref import ( ImportError: cannot import name _remove_dead_weakref Failed to load file 

As stated in the error log, this happens when I import :

from sklearn.externals import joblib 

NOTES

NOTE 1: calling import sklearn alone also produces this error.

NOTE 2: Running without any other import statements and the rest of the code in the file commented out also produces the error

NOTE 3: Objective-C code is calling this python file via the Python C API (https://docs.python.org/2/c-api/index.html). This works perfectly on another machine with the exact same specs and OS (see details below).

NOTE 4: There is no error if I call Python from the terminal then import sklearn OR from sklearn.externals import joblib (Without calling from objective-C)

NOTE 5: I don't think I have any virtual environments installed. I've tried calling pyenv, pyvenv and virtualenv on the terminal. Always getting -bash: pyvenv: command not found

DETAILS

Both machines I've tried this on are MacBook Pros with identical specs running Mac OS 10.13. (The code works on one of them and fails on the other)

I've tried this on the machine the code fails on :

I've added the following lines to both (a) the script called by the executable AND (b) on the python CLI on the terminal :

print ">>>>>>>>>"+str(sys.executable) print ">>>>>>>>>"+str(sys.version) print "++++++++++++++++++++++++++++++++++++++++++++++++++++" for p in sys.path:      print p 

Output from the script that's failing :

>>>>>>>>>/anaconda2/bin/python >>>>>>>>>2.7.10 (default, Feb  6 2017, 23:53:20)  [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] ++++++++++++++++++++++++++++++++++++++++++++++++++++ /anaconda2/lib/python27.zip /anaconda2/lib/python2.7 /anaconda2/lib/python2.7/plat-darwin /anaconda2/lib/python2.7/plat-mac /anaconda2/lib/python2.7/plat-mac/lib-scriptpackages /anaconda2/lib/python2.7/lib-tk /anaconda2/lib/python2.7/lib-old /anaconda2/lib/python2.7/lib-dynload /anaconda2/lib/python2.7/site-packages /anaconda2/lib/python2.7/site-packages/aeosa ~/Desktop/Test Software/Sources/*****/path/to/.py/file/*****/Classification 

Output on the terminal :

>>> sys.executable '/anaconda2/bin/python'  >>> sys.version '2.7.14 |Anaconda, Inc.| (default, Dec  7 2017, 11:07:58) \n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]'  >>> for p in sys.path: ...     print p ...   /anaconda2/lib/python27.zip /anaconda2/lib/python2.7 /anaconda2/lib/python2.7/plat-darwin /anaconda2/lib/python2.7/plat-mac /anaconda2/lib/python2.7/plat-mac/lib-scriptpackages /anaconda2/lib/python2.7/lib-tk /anaconda2/lib/python2.7/lib-old /anaconda2/lib/python2.7/lib-dynload /anaconda2/lib/python2.7/site-packages /anaconda2/lib/python2.7/site-packages/aeosa 

Basically, getting the same thing, except for the version, and that I'm (obviously) also seeing the path to the .py file in the search path.

2 Answers

Answers 1

Try to uninstall, then install again your package:

conda uninstall scikit-learn

Answers 2

Try following the below commands. I faced a similar issue I was able to resolve it using:

  1. sudo pip uninstall scikit-learn
  2. sudo pip install -U scikit-learn

Make sure following dependencies are met: Scikit-learn requires:

Python (>= 2.7 or >= 3.3) NumPy (>= 1.8.2), SciPy (>= 0.13.3)

Also checkout this Link.

For Note5: pyenv command does not exist in Python 2. You can use virtualenv instead.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment