134

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.

nlper
  • 2,297
  • 7
  • 27
  • 37
  • 3
    Why are you doing it within a shell script? Why aren't you doing it inside a Python file? – Ffisegydd Feb 13 '15 at 13:47
  • @Ffisegydd: I have one generalized rule, where in shellscript I check for all need packages, if any package does not exist, then install it other wise skip to next check. As I need to check and execute few other python as well shellscripts, I am using it. Is using `shellscript` for this is bad idea? – nlper Feb 13 '15 at 14:02
  • It'll be much more easy to test these exist using a Python script (imo). You could always call the Python script from a shell script, if you really wanted to. If you just use shell syntax then you'll have to work out where each package is located, find the package, then parse it's filename for the version numbers somehow. – Ffisegydd Feb 13 '15 at 14:04
  • @Ffisegydd: thanks, but in that case I have to write python script to validate each package i want to check and execute that script to validate it. as per what i understand. – nlper Feb 13 '15 at 14:12
  • Or you just put them all in one script? – Ffisegydd Feb 13 '15 at 14:13
  • @Ffisegydd: can you please say how can write install command in python script? like `sudo easy_install pip` in python script is possible? – nlper Feb 13 '15 at 14:28
  • simply use **print** nltk.__version__ instead of echo. – ashish Jan 14 '16 at 20:39

10 Answers10

207

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.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • @nlper For shellscript: Type `python` and `>>> import sklearn` and then `>>> sklearn.__version__` and you get the version printed on shell. Same for `nltk` – NewToCoding Jun 26 '19 at 11:15
31

Try this:

$ python -c "import nltk; print nltk.__version__"
Aaron
  • 2,383
  • 3
  • 22
  • 53
27

In Windows® systems you can simply try

pip3 list | findstr scikit

scikit-learn                  0.22.1

If you are on Anaconda try

conda list scikit

scikit-learn              0.22.1           py37h6288b17_0

And this can be used to find out the version of any package you have installed. For example

pip3 list | findstr numpy

numpy                         1.17.4
numpydoc                      0.9.2

Or if you want to look for more than one package at a time

pip3 list | findstr "scikit numpy"

numpy                         1.17.4
numpydoc                      0.9.2
scikit-learn                  0.22.1

Note the quote characters are required when searching for more than one word.

Take care.

10

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!

prabhat
  • 291
  • 3
  • 13
4

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.

kmario23
  • 57,311
  • 13
  • 161
  • 150
1

I needed to do the exact same thing and I found the fastest of the solutions proposed yet. Important: I have Anaconda installed.

In Terminal, type:

python3

Then:

import nltk

And:

nltk.__version__

OUTPUT I am seeing:

'3.5'

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ana_Ghost
  • 11
  • 1
0

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__))
Orhan Celik
  • 1,513
  • 15
  • 12
0

Do not use presort='deprecated' in the below code and it works.

DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='entropy',
                       max_depth=15, max_features=None, max_leaf_nodes=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=25, min_samples_split=25,
                       min_weight_fraction_leaf=0.0, presort='deprecated',
                       random_state=None, splitter='best')

Checked with version:

>>> import sklearn
>>> print('The scikit-learn version is {}.'.format(sklearn.__version__))
The scikit-learn version is 0.24.1.
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
little_amb
  • 84
  • 5
0

If it was installed via pip then run pip show scikit-learn on the command line.

mer iem
  • 1
  • 1
-1

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.

samsamara
  • 4,630
  • 7
  • 36
  • 66