当我运行我的Python脚本时,我会得到以下的警告
DeprecationWarning: the sets module is deprecated
我该如何解决这个问题?
当我运行我的Python脚本时,我会得到以下的警告
DeprecationWarning: the sets module is deprecated
我该如何解决这个问题?
停止使用 sets
模块,或者切换到一个旧版本的 Python,因为在较新的版本中这个模块已被弃用。
根据 PEP-0004,从 v2.6 开始,sets
已经被弃用,被内置的 set
和 frozenset
类型所替代。
历史:
Python 2.3之前:无集合功能
Python 2.3:引入sets
模块
Python 2.4:引入内置的set
和frozenset
Python 2.6:弃用sets
模块
你应该将你的代码改为使用内置的set
而非sets.Set
。
如果你仍希望支持Python 2.3,则可以在脚本开头添加以下内容:
try:
set
except NameError:
from sets import Set as set
如果你想修复它,James 绝对有正确的答案。但是如果你只想关闭弃用警告,你可以这样运行 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)
您不需要导入sets
模块就可以使用它们,因为它们在内置命名空间中。