Counter in Collections module Python

29,803

Solution 1

The Counter class was added to the module in Python 2.7. You are most likely using Python 2.6 or older. From the collections.Counter() documentation:

New in version 2.7.

On python 2.5 or 2.6, use this backport instead.

Solution 2

Came across the same issue while installing pandas.

Cause: Counter is only supported in python2.7 and higher and is not available in earlier versions - Counter class got added into collections package in Python 2.7.


Solution 1: As stated by Martin Pieters - use the backport.

Add counter.py at /lib64/python2.6/ - this is where the collections.py is ./lib64/python2.6/collections.py Patch collections.py with:

from counter import Counter

Solution 2: use the backport_collections package. Next patch (the import statement) the package you're getting exception at i.e. pandas in my case:

from backport_collections import Counter

Solution 3

You should use a new version of python AS python 3. You can then use this module. Then import,

import collections
from collections import counter

Solution 4

You're probably using an old version of Python, the Counter class, as stated in the documentation was added in version 2.7.

Share:
29,803
branwen85
Author by

branwen85

Updated on October 31, 2020

Comments

  • branwen85
    branwen85 over 3 years

    I've come across a really weird problem. I'm trying to use Counter function in collections module. However, I keep getting the same error message

    AttributeError: 'module' object has no attribute 'Counter'
    

    I have tried using it before and it worked fine, but now for some reason when I import "collections" module it has a very limited number of attributes.

    I have tried:

    import collections   # when calling Counter I would then use collections.Counter()
    import collections as collect # collect.Counter()
    

    For both of those I keep getting Attribute Error.

    I have also tried

    from collections import Counter
    

    And in this case I got:

    ImportError: cannot import name Counter
    

    These are all tested both in ipython interface and through a script (not importing anything else, just the collections).

    Any ideas?

    • Perkins
      Perkins over 11 years
      In general when dealing with this sort of unexpected behaviour, it is often a good idea to make sure the module is the one you think it is. So try import collections;print(collections) and make sure it is the standard library version. I've seen this sort of issue crop up when either the python path gets screwed up or a library gets added to the python path that has the same name as a standard library.
    • Ravi
      Ravi over 3 years
      make sure your file name should not be "collections.py" otherwise you will face consequences.
  • Kamal Nayan
    Kamal Nayan about 6 years
    I'm facing same issue in Python 3.0 as well, 'module' object has no attribute 'Counter': print(collections.Counter({'a':2, 'b':3, 'c':1}))
  • Martijn Pieters
    Martijn Pieters about 6 years
    @KamalNayan you’ll need 3.1 or newer. 3.0 is really ancient and I very, very strongly urge you not to use it. The Python 3 series started to become really great at 3.4 and newer. 3.0 has some serious performance issues for example.
  • Kamal Nayan
    Kamal Nayan about 6 years
    I'm beginner to python and is getting my hands dirty on different versions. I don't know what was issue on that windows PC (using py 3.0), it was not working, but same is working on my other windows system (on 2.7 and 3.6 both). I'll check on py 3.6 on my former PC.