gpt4 book ai didi

python - 从 scikit 学习导入时出现 `ImportError: cannot import name _remove_dead_weakref`

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:14 24 4
gpt4 key购买 nike

问题描述

调用 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 alone 也会产生此错误。

注 2:在没有任何其他导入语句的情况下运行并且文件中的其余代码被注释掉也会产生错误

注 3:Objective-C 代码通过 Python C API ( https://docs.python.org/2/c-api/index.html ) 调用这个 python 文件。这在具有完全相同规范和操作系统的另一台机器上完美运行(请参阅下面的详细信息)。

注意事项 4:如果我从终端调用 Python 然后 import sklearnfrom sklearn.externals import joblib 没有没有错误(没有从 objective-C 调用)

注 5:我认为我没有安装任何虚拟环境。我尝试在终端上调用 pyenvpyvenvvirtualenv。总是得到 -bash: pyvenv: command not found

详情

我试过的两台机器都是 MacBook Pro,具有相同的规范,运行 Mac OS 10.13。 (代码在其中一个上有效,在另一个上失败)

我已经在代码失败的机器上试过了:

我已将以下行添加到 (a) 可执行文件调用的脚本和 (b) 在终端的 python CLI 上:

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 文件的路径。

最佳答案

尝试卸载,然后重新安装您的包:

conda 卸载 scikit-learn

关于python - 从 scikit 学习导入时出现 `ImportError: cannot import name _remove_dead_weakref`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48973628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com