Unable to import nltk on mac os x
Question: I had successfully installed nltk [from this
site](http://www.nltk.org/install.html). And just to validate i am able to
import it from the terminal. But when i execute my python script from the
Spyder it gives me following error in Spyders terminal

    
    
    File "/Prateek/Python/RC_ISSUES/algorithm_RC.py", line 9, in <module>
    import nltk
    
    ImportError: No module named nltk
    

Below output is from the terminal

[![Importing nltk from the
terminal](http://i.stack.imgur.com/Y84Ij.png)](http://i.stack.imgur.com/Y84Ij.png)

I know there might be similar questions but i thought it is different from
rest of the other questions


Answer: When you execute a python script, the operating system is looking for the
interpreter as specified on the first line of the script, which most of the
time is:

    
    
    #!/usr/bin/python
    

On Mac OS X, this is the python as distributed with the system when you
installed it, which is the one distributed with the system. It is usually very
likely to be the one that has the older compilation date:

    
    
    2.7.10 (default, Jun 1 2015, 09:45:55) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
    

If you do `type python` in your shell, you're very likely to see another path
to that interpreter, e.g. if you installed the brew version of python:

    
    
    % type python
    python is /usr/local/bin/python
    

So you have two ways around it, either you explicitly launch your script with
python:

    
    
    python algorithm_RC.py
    

in doubt, use the full path you found out with `type`:

    
    
    /usr/local/bin/python algorithm_RC.py
    

or, you can change your script first line with:

    
    
    #!/usr/bin/env python
    

which will use the same python as the one you're reaching from your shell. You
can also use the full path to your manually installed python, by making that
line:

    
    
    #!/usr/local/bin/python
    

or whatever the `type` command gave. But I would advise you against that, as
the `/usr/bin/env` solution is more flexible and makes sure you're using in
both cases the same python from the shell and within the script.

Finally, you can also use the system's python by calling explicitly
`easy_install` from `/usr/bin`:

    
    
    sudo /usr/bin/pip nltk
    

And if you don't have pip there, then you'll have to install it first:

    
    
    sudo /usr/bin/easy_install pip
    

HTH

