简体   繁体   English

无法在 Jupyter Notebook 中导入 NLTK

[英]Can't import NLTK in Jupyter Notebook

I can import the nltk package when running python or ipython from bash.可以在从 bash 运行 python 或 ipython 时导入 nltk 包。 So, nltk is definitely installed somewhere (in python from bash, nltk.__file__ is /home/nadine/anaconda2/lib/python2.7/site-packages/nltk/__init__.pyc )所以,nltk 肯定安装在某个地方(在 bash 的 python 中, nltk.__file__/home/nadine/anaconda2/lib/python2.7/site-packages/nltk/__init__.pyc

However, when using Jupyter Notebook (which I installed using Anaconda, with the 2.7 version of python), importing nltk fails:但是,当使用 Jupyter Notebook(我使用 Anaconda 安装,使用 2.7 版本的 python)时,导入 nltk 失败:

import nltk
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-b06499430ee0> in <module>()
----> 1 import nltk

ImportError: No module named nltk

In Jupyter Notebook, sys.executable yields /home/nadine/.conda/envs/py27/bin/python , while in python from bash it yields /home/nadine/anaconda2/bin/python2.7在 Jupyter Notebook 中, sys.executable产生/home/nadine/.conda/envs/py27/bin/python ,而在来自 bash 的 python 中它产生/home/nadine/anaconda2/bin/python2.7

What exactly is going wrong here and how can I fix it?这里到底出了什么问题,我该如何解决?

Anaconda uses its own version of Python, and you clearly have installed the nltk in the library for system Python. Anaconda 使用自己的 Python 版本,您显然已经在系统 Python 的库中安装了nltk

But Anaconda normally comes bundled with the nltk -- why is yours absent?但是 Anaconda 通常与nltk捆绑在一起——为什么你的没有? Perhaps you installed a minimal version, and the nltk needs to be installed on top of it.也许您安装了最小版本,并且需要在其上安装nltk Check by running conda list nltk at the (anaconda-aware) bash prompt.通过在(anaconda-aware)bash 提示符下运行conda list nltk检查。

Whatever the reason, it sounds like the nltk is not there.不管是什么原因,听起来 nltk 不存在。 Install it with conda install nltk .使用conda install nltk安装它。

Had the same issue.有同样的问题。 I resolved it by another jupyter version.我通过另一个 jupyter 版本解决了它。

For me it did not work in the classic juypter notebook .对我来说,它在经典的juypter notebook I installed jupyterlab -> pip install jupyterlab and start it with: jupyter-lab .我安装了 jupyterlab -> pip install jupyterlab并使用以下jupyter-lab启动它: jupyter-lab

Summary:总结:

pip install jupyterlab
jupyter-lab

Then it worked!然后它起作用了!

Just a hunch, you can try and see which kernel your Jupyter Notebook is running.凭直觉,您可以尝试查看您的 Jupyter Notebook 正在运行哪个内核。 Perhaps it is not using the [conda root] and therefore doesn't have access to the package.也许它没有使用 [conda root],因此无法访问包。

You should be able to see it in the top-right corner of the Notebook screen.您应该能够在 Notebook 屏幕的右上角看到它。 Otherwise go to Kernel>Change Kernel in the menu and check there.否则转到菜单中的Kernel>Change Kernel并检查那里。

It's possible that your conda environment is set up with the wrong version of Python.您的 conda 环境可能设置了错误的 Python 版本。 I had this problem and it was resolved by:我遇到了这个问题,它是通过以下方式解决的:

  • checking the version of python running in the notebook with sys.version使用sys.version检查笔记本中运行的 python 版本
  • deactivating the current environment停用当前环境
  • creating a new environment with that python package使用该 python 包创建一个新环境
  • activating the new environment激活新环境
  • running the notebook, using import nltk and nltk.download()运行笔记本,使用import nltknltk.download()

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

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