如何检查nltk的哪个版本,scikit学习安装?

nlp*_*per 78 python linux shell nltk scikit-learn

在shell脚本中,我正在检查是否已安装此软件包,如果未安装,则安装它.所以使用shell脚本:

import nltk
echo nltk.__version__
Run Code Online (Sandbox Code Playgroud)

但在停止shell脚本import

在linux终端试图以这种方式看到:

which nltk
Run Code Online (Sandbox Code Playgroud)

没有任何想法它安装.

有没有其他方法可以在shell脚本中验证此软件包安装,如果没有安装,也可以安装它.

Ffi*_*ydd 124

import nltk 是Python语法,因此在shell脚本中不起作用.

为了测试的版本nltkscikit_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.
Run Code Online (Sandbox Code Playgroud)

请注意,并非所有Python包都保证具有__version__属性,因此对于其他一些包可能会失败,但对于nltk和scikit-learn,至少它会起作用.


Aar*_*ron 26

试试这个:

$ python -c "import nltk; print nltk.__version__"
Run Code Online (Sandbox Code Playgroud)

  • @nlper,这是完全有效的`shell`代码.我认为这个答案没有任何问题.注意,您可以使用`var = $(<some command>)`将输出保存到变量中 (6认同)
  • @cel:对不起,明白了.没有足够的信用来支持回答:) (2认同)

agc*_*ala 10

你可以试试

pip3 list
Run Code Online (Sandbox Code Playgroud)

这会给你一个像这样的清单

bleach (2.0.0)
colorama (0.3.9)
cycler (0.10.0)
decorator (4.1.2)
entrypoints (0.2.3)
enum34 (1.1.6)
graphviz (0.8)
html5lib (0.999999999)
ipykernel (4.6.1)
ipython (6.1.0)
ipython-genutils (0.2.0)
ipywidgets (7.0.0)
jedi (0.10.2)
Jinja2 (2.9.6)
  ..........
PyYAML (3.12)
pyzmq (16.0.2)
qtconsole (4.3.1)
scikit-learn (0.19.0)   <------
scipy (0.19.1)
setuptools (36.4.0)
simplegeneric (0.8.1)
   .......
Run Code Online (Sandbox Code Playgroud)

您可以直观地扫描列表以查找所有已安装软件包的版本...列表按字母顺序排列,因此很容易扫描.

  • TRY:$ pip list | grep scikit* - 为什么在grep更快时搜索很长时间. (3认同)

pra*_*hat 5

要检查shell脚本中scikit-learn的版本,如果已安装pip,则可以尝试以下命令

pip freeze | grep scikit-learn
scikit-learn==0.17.1
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!