ImportError: cannot import name porter python

amits picture amits · Jul 11, 2015 · Viewed 8.6k times · Source

I am importing nltk library in my project and it is giving following error. If someone has same error, please help.

        Traceback (most recent call last):
  File "/home/nitai/Dropbox/thesis/PycharmProjects/auto_tagger2/tagger.py", line 4, in <module>
    import buildVocab
  File "/home/nitai/Dropbox/thesis/PycharmProjects/auto_tagger2/buildVocab.py", line 4, in <module>
    import nltk
  File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 126, in <module>
    from nltk.stem import *
  File "/usr/local/lib/python2.7/dist-packages/nltk/stem/__init__.py", line 29, in <module>
    from nltk.stem.snowball import SnowballStemmer
  File "/usr/local/lib/python2.7/dist-packages/nltk/stem/snowball.py", line 25, in <module>
    from nltk.stem import porter
ImportError: cannot import name porter

I have installed all nltk packages. I don't know.

Answer

alvas picture alvas · Jul 13, 2015

If you have downloaded all data packages from NLTK, i.e.

>>> import nltk
>>> nltk.download('all')

Porter Stemmer in NLTK is a class not a package/module.

You should import PorterStemmer class instead:

>>> from nltk.stem import PorterStemmer
>>> porter = PorterStemmer()
>>> porter.stem('went')
u'went'
>>> porter.stem('running')
u'run'

Also, check that you have the latest stable version of NLTK by installing with pip. Otherwise, you might be using an unstable/outdated version of NLTK. See http://www.nltk.org/install.html