在 shell 脚本中,我正在检查是否安装了此软件包,如果未安装则安装它。所以使用 shell 脚本:
import nltk
echo nltk.__version__
但它会在 import
行停止 shell 脚本
在linux终端尝试用这种方式查看:
which nltk
这并没有让人觉得它已经安装了。
有没有其他方法可以在shell脚本中验证这个包安装,如果没有安装,也安装它。
最佳答案
import nltk
是 Python 语法,因此在 shell 脚本中不起作用。
要测试 nltk
和 scikit_learn
的版本,您可以编写一个 Python 脚本 并运行它。这样的脚本可能看起来像
import nltk
import sklearn
print('The nltk version is {}.'.format(nltk.__version__))
print('The scikit-learn version is {}.'.format(sklearn.__version__))
# The nltk version is 3.0.0.
# The scikit-learn version is 0.15.2.
请注意,并非所有 Python 包都保证具有 __version__
属性,因此对于其他一些包可能会失败,但对于 nltk 和 scikit-learn 至少它会起作用。
https://stackoverflow.com/questions/28501072/