Python - cannot import Set from sets ("no module named sets")

I am 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 64 bit .dmg installer.

I have successfully configured the Python interpreter (or, I think, since I can really 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 also checked it on the command line and gave me the same error.

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

I also tried copying 2.7 sets.py to 3.2, but it does not work ... Please, do you know what should I do?

+5
source share
3 answers

You no longer need the sets module. set is a built-in class in Python 3 and can be used without import.

 mySet = set() 
+17
source

In each of the latest version sets, python is built in as set , and Python 3 completely eliminated the legacy sets module.

If you want the code to work with ancient versions as well, you could do something like this:

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

If you need to run the old code and do not want to change it ( bad! ):

 try: from sets import Set except ImportError: Set = set 
+13
source

you do not 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 version 2.6

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

All Articles