Can't import NLTK in Jupyter Notebook
Asked Answered
J

4

8

I can import the nltk package when running python or ipython from bash. So, nltk is definitely installed somewhere (in python from bash, nltk.__file__ is /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:

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

What exactly is going wrong here and how can I fix it?

Jernigan answered 17/10, 2017 at 14:27 Comment(1)
Just check what version of python the Notebook is using by checking sys.version and install nltk to that sepcific python version. Then it works.Highlands
S
3

Anaconda uses its own version of Python, and you clearly have installed the nltk in the library for system Python.

But Anaconda normally comes bundled with the nltk-- why is yours absent? Perhaps you installed a minimal version, and the nltk needs to be installed on top of it. Check by running conda list nltk at the (anaconda-aware) bash prompt.

Whatever the reason, it sounds like the nltk is not there. Install it with conda install nltk.

Spidery answered 17/10, 2017 at 17:2 Comment(0)
H
2

Had the same issue. I resolved it by another jupyter version.

For me it did not work in the classic juypter notebook. I installed jupyterlab -> pip install jupyterlab and start it with: jupyter-lab.

Summary:

pip install jupyterlab
jupyter-lab

Then it worked!

Haarlem answered 6/12, 2020 at 0:59 Comment(1)
After looking into many other SO answers, this one made my day!Decorator
V
0

Just a hunch, you can try and see which kernel your Jupyter Notebook is running. Perhaps it is not using the [conda root] and therefore doesn't have access to the package.

You should be able to see it in the top-right corner of the Notebook screen. Otherwise go to Kernel>Change Kernel in the menu and check there.

Venenose answered 17/10, 2017 at 14:38 Comment(3)
There I can only see that it is running "Python 2". Is there any way of getting more detailed information?Jernigan
you can check help>about to see which version python your Jupyter Notebook is running. For me it says: "Python 2.7.13 |Anaconda, Inc.|" for example. You can also use the env command within ipython (where the import works) and jupyter (where it doesn't) to check the details of the environment you are currently in.Venenose
Another way to check the python version you're using is sys.version (with import sys).Venenose
E
0

It's possible that your conda environment is set up with the wrong version of Python. I had this problem and it was resolved by:

  • checking the version of python running in the notebook with sys.version
  • deactivating the current environment
  • creating a new environment with that python package
  • activating the new environment
  • running the notebook, using import nltk and nltk.download()
Eckmann answered 18/5, 2020 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.