2010-01-11 49 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)