2010-01-11 51 views
25

当我跑我的Python脚本,我得到以下警告设置模块弃用警告

DeprecationWarning: the sets module is deprecated 

我该如何解决这个问题?

+0

哪个版本的Python? – 2010-01-11 08:27:37

+0

Python版本2.6.4 – Dave 2010-01-11 08:28:15

回答

33

停止使用sets模块,或切换到不推荐使用的较旧版本的python。

根据pep-004sets已从v2.6弃用,取而代之的是内置的set and frozenset types

+4

+1:通过修复导致警告的问题修复警告。看起来很简单。 – 2010-01-11 11:15:13

+1

它只是看起来很简单,如果你知道有一个内置的替换它。为什么警告不这么说? – GreenAsJade 2016-01-08 11:10:59

2

您不需要导入sets模块以使用它们,它们位于内置命名空间中。

4

使用builting set代替进口套模块

documentation

该套模块已弃用; 最好使用内置集合 和定位类型。

25

历史:

的Python 2.3之前:没有设定功能
的Python 2.3:sets模块赶到
的Python 2.4:setfrozenset内置插件推出
的Python 2.6:sets模块弃用

您应该更改您的代码以使用set而不是sets.Set

如果您仍然希望能够使用Python 2.3的支持,你可以在你的脚本开始这样做:

try: 
    set 
except NameError: 
    from sets import Set as set 
5

如果你要修复它詹姆斯绝对有正确的答案,但在如果你只想关闭废弃警告,你可以像这样运行python:

$ python -Wignore::DeprecationWarning 
Python 2.6.2 (r262:71600, Sep 20 2009, 20:47:22) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import sets 
>>> 

(来源:http://puzzling.org/logs/thoughts/2009/May/3/python26-deprecation-warning

您也可以忽略它编程:

import warnings 
warnings.simplefilter("ignore", DeprecationWarning)