问题说明
我在调用python脚本时遇到此错误:
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
如错误日志中所述,导入时会发生这种情况:
from sklearn.externals import joblib
备注
注1:
单独调用import sklearn
也会产生此错误。
注2: 在没有任何其他import语句的情况下运行并且文件中的其余代码被注释掉也会产生错误
注3: Objective-C代码通过Python C API(https://docs.python.org/2/c-api/index.html)调用此python文件。 这完全适用于具有完全相同规格和操作系统的另一台机器(详见下文)。
注4:
如果我从终端调用Python然后import sklearn
或from sklearn.externals import joblib
(没有从Objective-C调用)
注5:
我不认为我安装了任何虚拟环境。我尝试在终端上拨打pyenv
,pyvenv
和virtualenv
。始终获得-bash: pyvenv: command not found
详情
我试过的两台机器都是MacBook Pro,运行Mac OS 10.13的规格相同。 (代码适用于其中一个而另一个则失败)
我已经在机器上尝试了这个,代码失败了:
我已将以下行添加到(a)终端上python CLI上可执行文件AND(b)调用的脚本中:
print ">>>>>>>>>"+str(sys.executable)
print ">>>>>>>>>"+str(sys.version)
print "++++++++++++++++++++++++++++++++++++++++++++++++++++"
for p in sys.path:
print p
脚本输出失败:
>>>>>>>>>/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
终端输出:
>>> 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
基本上,获得相同的东西,除了版本,并且我(显然)也在搜索路径中看到.py文件的路径。
答案 0 :(得分:1)
尝试卸载,然后重新安装您的软件包:
conda uninstall scikit-learn
答案 1 :(得分:0)
请尝试按照以下命令操作。我遇到了类似的问题,我可以使用以下方法解决它:
sudo pip uninstall scikit-learn
sudo pip install -U scikit-learn
确保符合以下依赖关系: Scikit-learn需要:
Python (>= 2.7 or >= 3.3)
NumPy (>= 1.8.2),
SciPy (>= 0.13.3)
另请查看此Link。
注意事项5:
pyenv
中不存在Python 2
命令。
您可以改用virtualenv
。