Warm tip: This article is reproduced from stackoverflow.com, please click
nltk python python-2.7 syntax syntax-error

Invalid Syntax when I imported nltk in python 2.7

发布于 2020-07-24 23:05:45

when I wrote the line

import nltk

in python 2.7 interpreter terminal it is showing SyntaxError:Invalid Syntax

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/nani/.local/lib/python2.7/site-packages/nltk/__init__.py", line 128, in <module>
from nltk.collocations import *
File "/home/nani/.local/lib/python2.7/site-packages/nltk/collocations.py", line 35, in <module>
from nltk.probability import FreqDist
File "/home/nani/.local/lib/python2.7/site-packages/nltk/probability.py", line 333
print("%*s" % (width, samples[i]), end=" ")                                      ^
SyntaxError: invalid syntax

How to fix this?

Questioner
Nani Babu
Viewed
243
Nani Babu 2020-05-04 00:03

So nltk dropped support to Python2, Try to use older versions of nltk in which it supports python 2 and I found out that nltk 3.0 version supports python 2 [edited - Thanks to user2357112 supports Monica ]

So, Try to download and install previous versions of nltk with the command

pip install nltk==3.0

You can change the version number which is 3.0 int the above mentioned case and can install suitable version whichever you feels working.

It worked for me.If anyone facing same problem can try above mentioned method.