简体   繁体   English

Python 找不到模块 NLTK

[英]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我按照这些说明http://www.nltk.org/install.html在我的mac(10.6)上安装nltk模块我已经安装了python 2.7,但是当我打开IDLE并输入import nltk时它给了我这个错误

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.问题是该模块安装在另一个 python 版本 2.6 中。 How can I install the package in python version 2.7?如何在 python 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然后使用命令再次安装 NLTK

sudo pip install -U nltk

but I get the message: Requirement already up-to-date in /Library/Python/2.6/.但我收到消息:/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.所以显然命令行export PYTHONPATH没有做任何事情(它仍然尝试在 2.6 中安装包)或者(更有可能)我不理解该命令行的含义/功能。 What am I doing wrong?我究竟做错了什么?

On OS X you could have multiple installation of Python, so investigate it first:在 OS X 上,您可以安装多个 Python,因此请先进行调查:

$ 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. /usr/bin中的所有内容都是内置的, /usr/local/bin中的所有其他内容都是由 Homebrew 或其他一些包管理器外部安装的。

If you're using pip or pip3 from /usr/local , then you've to use the same Python instance, otherwise they're different instances.如果您使用来自/usr/localpippip3 ,那么您必须使用相同的 Python 实例,否则它们是不同的实例。

Just install it via pip :只需通过pip安装它:

pip install nltk

or for Python 3:或者对于 Python 3:

pip3 install nltk

then run the right Python instance from /usr/local/bin or update your PATH system variable.然后从/usr/local/bin运行正确的 Python 实例或更新您的PATH系统变量。

Make sure you install the actual Python for Mac, not the one built into the console.确保您安装了实际的 Python for Mac,而不是控制台内置的。 Then, install pip by executing this script.然后,通过执行脚本安装 pip。 Then skip to part 3 of the instructions and go from there.然后跳到说明的第 3 部分并从那里开始。

尝试这个

pip install --user -U nltk

我会使用 virtualenv,但如果你真的想从终端使用它,我建议将你的导出语句添加到 ~/.bashrc

On my mac I had two different versions of Python 3 installed: Python 3.6 and Python 3.7.在我的 Mac 上,我安装了两个不同版本的 Python 3:Python 3.6 和 Python 3.7。 I had installed nltk having Python 3.7 in my $PATH :我在我的$PATH中安装了具有 Python 3.7 的nltk

$ 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.但是 Python 3.6 缺少nltk Solution: install nltk also for version 3.6.解决方案:也为 3.6 版安装nltk

$ 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

只需在安装后重新启动 Jupyter Notebook 或您正在使用的任何东西......它就可以工作......

I had a similar issue an an Intel MacBook.我在英特尔 MacBook 上遇到了类似的问题。

Fixed with固定与

  1. Make sure the python bin directory in the path确保路径中的python bin目录
    • mine is /Users/<user>/Library/Python/3.9/bin我的是/Users/<user>/Library/Python/3.9/bin
  2. Restart the computer重新启动计算机

NOTE that just restarting the terminal did NOT help请注意,仅重新启动终端并没有帮助

In my case, the following command worked for me.就我而言,以下命令对我有用。 Try this!!尝试这个!!

py -3 -m pip install nltk

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM