28

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 installer.

I configured the Python interpreter successfully (or I think so, as I actually can create a "hello world" project and run it). But for some reason, when I try to import Set (from sets import Set) I get this error:

    from sets import Set;
ImportError: No module named sets

I have tested it on command line too, and gives me the same error.

Then I have looked at the lib folder from my python3 directory (under /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/) and it's missing sets.py file!!! The original 2.7 version does have it at /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/

I have also tried to copy the 2.7 sets.py to the 3.2, but it neither works... Please, do you know what have I to do?

3
  • 3
    It sounds like you're reading a tutorial that is meant for (an ancient version of) Python 2, rather than one meant for Python 3. You should either switch tutorials, or switch to a version of Python 2 and switch tutorials to something more modern.
    – Julian
    Aug 19, 2012 at 16:56
  • 1
    I would highly recommend never copying around python standard modules between different versions into their own standard lib locations. If you start to do that, you have to imagine you must be doing something wrong. The std libs installs dont need user maintenance.
    – jdi
    Aug 19, 2012 at 16:56
  • I know that. I'm just desperated and wanted to try (I had hope ;) ) Aug 19, 2012 at 17:01

3 Answers 3

61

You don't need the sets module anymore. set is a built-in class in Python 3 and can be used without import.

my_set = set()
2
  • Thanks a lot! I also tried to use it directly, but I was spelling it with upper case Set(). Great stackoverflow :) Aug 19, 2012 at 17:03
  • @RicardPérezdelCampo: I cannot think of any standard library method that starts with a capital letter.
    – Lenna
    Aug 19, 2012 at 17:59
21

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
1
  • 2
    I would be somewhat surprised to learn that old code that depends on the sets module would work on a python version without it (py3) with no further changes other than adding the import line. Anyways; nice answer, +1! Aug 19, 2012 at 17:02
1

you don't need to use

from sets import Set
engineers = Set(['John', 'Jane', 'Jack', 'Janice'])

above is Deprecated since version 2.6:

you can use below code above 2.6 version

engineers = set(['John', 'Jane', 'Jack', 'Janice'])
programmers = set(['Jack', 'Sam', 'Susan', 'Janice'])
managers = set(['Jane', 'Jack', 'Susan', 'Zack'])
employees = engineers | programmers | managers  
print(employees)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.