温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Invalid Syntax when I imported nltk in python 2.7
nltk python python-2.7 syntax syntax-error

其他 - 在python 2.7中导入nltk时语法无效

发布于 2020-08-14 17:12:45

当我写那行

import nltk

在python 2.7解释器终端中它显示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

如何解决这个问题?

查看更多

提问者
Nani Babu
被浏览
699
Nani Babu 2020-05-04 00:03

因此nltk放弃了对Python2的支持,尝试使用nltk的旧版本来支持python 2,我发现nltk 3.0版本支持python 2 [编辑-感谢user2357112支持Monica ]

因此,尝试使用以下命令下载并安装nltk的早期版本

pip install nltk==3.0

您可以将上述情况下的版本号更改为3.0,并可以根据需要安装合适的版本。

它对我有用,如果有人遇到相同的问题可以尝试上述方法。