Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
998 views
in Technique[技术] by (71.8m points)

macos - Python can't find module NLTK

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...