可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
In shell script I am checking whether this packages are installed or not, if not installed then install it. So withing shell script:
import nltk
echo nltk.__version__
but it stops shell script at import
line
in linux terminal tried to see in this manner:
which nltk
which gives nothing thought it is installed.
Is there any other way to verify this package installation in shell script, if not installed, also install it.
回答1:
import nltk
is Python syntax, and as such won't work in a shell script.
To test the version of nltk
and scikit_learn
, you can write a Python script and run it. Such a script may look like
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.
Note that not all Python packages are guaranteed to have a __version__
attribute, so for some others it may fail, but for nltk and scikit-learn at least it will work.
回答2:
Try this:
$ python -c "import nltk; print nltk.__version__"
回答3:
You can simply try
pip3 list
and that would give you a list like this
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)
.......
You can visually scan the list to find the version of all installed packages... the list is in alphabetical order, so it is easy to scan.
If you are in Anaconda conda list
would do the same for you.
回答4:
For checking the version of scikit-learn in shell script, if you have pip installed, you can try this command
pip freeze | grep scikit-learn
scikit-learn==0.17.1
Hope it helps!
回答5:
You can find NLTK version simply by doing:
In [1]: import nltk
In [2]: nltk.__version__
Out[2]: '3.2.5'
And similarly for scikit-learn,
In [3]: import sklearn
In [4]: sklearn.__version__
Out[4]: '0.19.0'
I'm using python3 here.
回答6:
you may check from a python notebook cell as follows
!pip install --upgrade nltk # needed if nltk is not already installed
import nltk
print('The nltk version is {}.'.format(nltk.__version__))
print('The nltk version is '+ str(nltk.__version__))
and
#!pip install --upgrade sklearn # needed if sklearn is not already installed
import sklearn
print('The scikit-learn version is {}.'.format(sklearn.__version__))
print('The scikit-learn version is '+ str(nltk.__version__))
回答7:
In my machine which is ubuntu 14.04 with python 2.7 installed, if I go here,
/usr/local/lib/python2.7/dist-packages/nltk/
there is a file called
VERSION
.
If I do a cat VERSION
it prints 3.1
, which is the NLTK version installed.