Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named 'nltk.tokenize'; 'nltk' is not a package

I am using python 3.5.2 within the pycharm IDE in windows 7, and I am having trouble importing the nltk package.

import nltk

gives the following error:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs)
File "C:\Python\nltk practice.py", line 7, in <module> from nltk.tokenize import sent_tokenize, word_tokenize
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs)
ImportError: No module named 'nltk.tokenize'; 'nltk' is not a package

nltk seems to be properly installed. When i run the following in the terminal

pip install nltk

I get:

Requirement already satisfied (use --upgrade to upgrade): nltk in c:\users\leee\appdata\local\programs\python\python35-32\lib\site-packages

When I run PATH in the terminal, or sys.path in the python console,

C:\Users\leee\AppData\Local\Programs\Python\Python35-32\Lib\site-packages shows up within the long list. That is where all my other packages are installed, and other packages which are all importing properly.

I'm pretty confused right now, and all help is appreciated.

like image 764
Lee88 Avatar asked Nov 01 '16 13:11

Lee88


People also ask

How do you solve no modules named nltk?

The Python "ModuleNotFoundError: No module named 'nltk'" occurs when we forget to install the nltk module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install nltk command.

How do I download nltk from Pycharm?

Select your current project. Click the Python Interpreter tab within your project tab. Click the small + symbol to add a new library to the project. Now type in the library to be installed, in your example "nltk" without quotes, and click Install Package.

How do I download Punkt?

punkt is the required package for tokenization. Hence you may download it using nltk download manager or download it programmatically using nltk. download('punkt') .

Why do I get modulenotfounderror - no module named ‘NLTK’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘nltk’ error Trying to use the module- without installing the nltk package. If the IDE is set to the incorrect version of the Python/Python interpreter. You are using the virtual environment and the nltk module is not installed inside a virtual environment

How do I install NLTK in Python?

nltk is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it. NLTK is a leading platform for building Python programs to work with human language data.

Why can't I install NLTK in my Virtual Environment?

You're using two different versions of python, and you probably installed nltk in your root environment, but not your virtual environment. When you "activate" the environment called tensorflow, you are using a different virtual environment, in which you haven't installed nltk.

Why is NLTK not working in my messages?

If you look closely at your messages you will see that the successful import of nltk is on Python 3.6.3 and the failed import is on Python 3.5.2. This indicates that you have two Python installations of different versions, and nltk is installed in one but not in the other. Thanks for contributing an answer to Stack Overflow!


4 Answers

With conda 4.5.4 MosesTokenizer has been moved out of NLTK due to licensing issues #306 https://github.com/pytorch/text/issues/306

now what you have to do is

pip install mosestokenizer

and replace

from nltk.tokenize.moses import MosesTokenizer, MosesDetokenizer

with

from mosestokenizer import MosesTokenizer, MosesDetokenizer

if you are using conda please note that moses is removed form there and now available at on PyPI

like image 97
Rajat Avatar answered Sep 24 '22 21:09

Rajat


Thanks. It solved my problem.

Problem: I created a file "nltk.py" and writing code in that file and try to execute it is giving the error 'ModuleNotFoundError: No module named 'nltk.tokenize'; 'nltk' is not a package'.

Solution: After that I renamed "nltk.py" to some other then my problem got resolved.

like image 36
Tarak Avatar answered Sep 25 '22 21:09

Tarak


This usually happens because you have another file called nltk.py. Check your directory (C:\Python, where you are running this script) and remove or rename it if it's there. (I suppose the stray nltk.py might be somewhere else on your PYTHONPATH too.)

like image 41
alexis Avatar answered Sep 22 '22 21:09

alexis


In pycharm, press on ctrl/cmd + shift + A, then type "Python Interpreter"

and make sure you have the same interpreter as the one your pip refers to (and not some Jetbrains default one)

Note: If you have both python 2.7 and python 3.x installed, the convention is that pip refers to the 2.x dist, and pip3 refers to 3.x

like image 40
Uri Goren Avatar answered Sep 25 '22 21:09

Uri Goren