为什么此弃用警告会停止代码执行?

Why is this Deprication Warning halting code execution?

我尝试使用 Sci-Kit Learn 包中的 TfidifVectorizer 和 CountVectorizer,但是当我导入它们时:
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

我收到以下警告消息:

/anaconda3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py:17: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import Mapping, defaultdict

我的代码在此之后停止 运行,即使该消息只是一个警告,表明出现错误(即使没有错误报告)。我想这是我询问警告的真正原因,因为这是我必须离开的全部。 这是 SKLearn 的错误吗?自从更新到python3.7,开发者落后了吗?任何关于我是否应该报告这个,或者如何使用 anaconda 恢复到 python 3.6 来解决这个问题的建议将不胜感激。谢谢!

此弃用警告引起问题的可能性很小。默认情况下,所有警告只打印一条消息然后继续。可以将警告视为异常,但随后您会看到堆栈跟踪而不是警告文本。弃用警告适用于开发人员,通知他们他们的库将在未来的 python 版本中中断。它们不适用于最终用户,因为代码仍然可以正常工作。举个例子,根据警告,from collections import Mapping, defaultdict 在您的 python (3.7) 版本中仍然有效,但在 python 3.8.

中无效

因此,此警告不太可能是问题的根源。你看到它是因为 sklearn 更改了默认警告过滤器,以便它的用户将看到 sklearn 发出的弃用警告。 警告不会改变执行流程,除非它们被设置为被视为错误。 要验证警告不是问题,您可以尝试 运行 您的程序在此工具中。它相当 hacky,但需要停止 sklearn 覆盖默认警告过滤器。通过使用警告过滤器的值,您应该能够看到弃用警告不是问题的根源。

import warnings
from warnings import (
    filterwarnings as original_filterwarnings, 
    simplefilter as original_simplefilter
)

def ignore_filterwarnings(*args, **kwargs):
    pass

def ignore_simplefilter(*args, **kwargs):
    pass

warnings.filterwarnings = ignore_filterwarnings
warnings.simplefilter = ignore_simplefilter

# no imports of sklearn should occur before the previous two lines (otherwise sklearn
# will get to use the original functions).

with warnings.catch_warnings():
    original_simplefilter("ignore")  # or "error" to see the difference

    from my_main_module import main
    main()