[英]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:
我遇到了这个问题,它是通过以下方式解决的:
sys.version
sys.version
检查笔记本中运行的 python 版本import nltk
and nltk.download()
import nltk
和nltk.download()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.