Python - can't import Set from sets (“no module named sets”)

前端 未结 3 1166
北海茫月
北海茫月 2021-01-03 19:46

I\'m trying to learn python and I was trying to write something simple. I am developing under Pydev (Eclipse) using OS X 10.8. I installed python 3.2 using the 64bits .dmg i

3条回答
  •  醉话见心
    2021-01-03 20:40

    In every recent python version sets are builtin as set and Python 3 got rid of the deprecated sets module altogether.

    If you wanted to ensure that the code also works with ancient versions you could do something like this though:

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

    If you need to run old code and don't want to change it (bad!):

    try:
        from sets import Set
    except ImportError:
        Set = set
    

提交回复
热议问题