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.
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.
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.
punkt is the required package for tokenization. Hence you may download it using nltk download manager or download it programmatically using nltk. download('punkt') .
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
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.
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.
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!
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
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.
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.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With