Python can't find module NLTK
Asked Answered
S

9

37

I followed these instructions http://www.nltk.org/install.html to install nltk module on my mac (10.6) I have installed python 2.7, but when I open IDLE and type import nltk it gives me this error

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import nltk
ImportError: No module named nltk

The problem is the module is installed in another python version, 2.6. How can I install the package in python version 2.7? I tried some of the solutions suggested in various answers, for example I tried typing this in the terminal

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

and then installed NLTK again with the command

sudo pip install -U nltk

but I get the message: Requirement already up-to-date in /Library/Python/2.6/. So apparently the command line export PYTHONPATH didn't do anything (it still tries to install the package in 2.6) OR (more likely) I didn't understand the meaning/functioning of that command line. What am I doing wrong?

Squeak answered 14/1, 2015 at 16:9 Comment(2)
Apple ships its own version of Python with OS X, that's why pip gets confused. There are probably ways to install it to your python2.7 installation, but the good solution is using a virtualenv.Shrivel
#19642868Noontide
E
62

On OS X you could have multiple installation of Python, so investigate it first:

$ which python python2 python3
/usr/bin/python
/usr/local/bin/python3

$ which pip pip2 pip3
/usr/local/bin/pip
/usr/local/bin/pip2
/usr/local/bin/pip3

All within /usr/bin are built-in and all other in /usr/local/bin are external installed by Homebrew or some other package manager.

If you're using pip or pip3 from /usr/local, then you've to use the same Python instance, otherwise they're different instances.

Just install it via pip:

pip install nltk

or for Python 3:

pip3 install nltk

then run the right Python instance from /usr/local/bin or update your PATH system variable.

Edlun answered 28/7, 2015 at 15:38 Comment(0)
S
7

Make sure you install the actual Python for Mac, not the one built into the console. Then, install pip by executing this script. Then skip to part 3 of the instructions and go from there.

Swanner answered 14/1, 2015 at 16:25 Comment(0)
H
6

In my case, the following command worked for me. Try this!!

py -3 -m pip install nltk

enter image description here

Hispanicize answered 15/6, 2022 at 14:23 Comment(0)
S
4

On my mac I had two different versions of Python 3 installed: Python 3.6 and Python 3.7. I had installed nltk having Python 3.7 in my $PATH:

$ pip3 install nltk
$ which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

But nltk was missing for Python 3.6. Solution: install nltk also for version 3.6.

$ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
$ pip3 install nltk
Collecting nltk
  Using cached https://files.pythonhosted.org/packages/6f/ed/9c755d357d33bc1931e157f537721efb5b88d2c583fe593cc09603076cc3/nltk-3.4.zip
Requirement already satisfied: six in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from nltk) (1.12.0)
Collecting singledispatch (from nltk)
  Using cached https://files.pythonhosted.org/packages/c5/10/369f50bcd4621b263927b0a1519987a04383d4a98fb10438042ad410cf88/singledispatch-3.4.0.3-py2.py3-none-any.whl
Installing collected packages: singledispatch, nltk
  Running setup.py install for nltk ... done
Successfully installed nltk-3.4 singledispatch-3.4.0.3
Sloshy answered 23/2, 2019 at 20:13 Comment(0)
U
4

Try this

pip install --user -U nltk
Untruthful answered 12/8, 2020 at 13:10 Comment(0)
F
2

I would use a virtualenv, but if you really want to use it from the terminal, I'd recommend adding your export statement to ~/.bashrc

Fatherland answered 14/1, 2015 at 16:13 Comment(0)
B
1

Just restart Jupyter Notebook or anything you are using after installing... It works...

Beamer answered 19/10, 2020 at 15:2 Comment(0)
I
1

I had a similar issue an an Intel MacBook.

Fixed with

  1. Make sure the python bin directory in the path
    • mine is /Users/<user>/Library/Python/3.9/bin
  2. Restart the computer

NOTE that just restarting the terminal did NOT help

Indoeuropean answered 22/8, 2021 at 22:44 Comment(1)
It's weired. But it works :D Setting the path seems to do the trick.Buyse
G
0

VERY chaotic this post. Let´s start by clearing some key points:

Hope this helps!

LAO

Griffe answered 22/11, 2023 at 16:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.