[Fixed] Nameerror: Name NLTK is not Defined

The users who work with natural language processing (NLP) often use the nltk library. The “nameerror: name nltk is not defined” error occurs when the interpreter can not find the nltk module. Read further to get a detailed elucidation of the error and method of resolution.

Primary Method of resolving the error

The given code shows how you can import the nltk library. Even if you have installed the library, importing is necessary. After that, you can carry out normal functionalities like downloading the punkt corpus. This is later used for tokenization of data.

import nltk

Other ways of installing nltk

If you have pip3 or python3 without pip or Windows or a Jupyter notebook, use the given commands accordingly.

pip3 install nltk

python3 -m pip install nltk

py -m pip install nltk

!pip install nltk

In case you are an anaconda user, opt for this command:

conda install -c anaconda nltk

Difference in versions

There might be a difference between the Python version you have installed and the version used for nltk installation. This leads to the nameerror: name nltk is not defined error that has been mentioned above. Check the Python path with this command. Then, using the path, install nltk to the third command.

import sys
/path -m pip install nltk

Usage of the virtual environment

If all these methods don’t work, you can switch to a virtual environment. These will help you prevent the nameerror: name nltk is not defined error.

#  python version can be changed according to your installed version
python3 -m venv venv

#  for Unix or MacOS
source venv/bin/activate

#  for Windows (cmd.exe)

#  for Windows (PowerShell)

pip install nltk

Upgrade nltk

At times, the nltk updated version may not exist on your system. This can also be a reason for the nameerror: name nltk is not defined error. Thus, you should consider upgrading it. The given command will install the upgraded version of nltk.

pip install nltk --upgrade
# or
python -m pip install nltk --upgrade

Solving the error in the conda environment

You might encounter the nameerror: name nltk is not defined while working in a conda environment. You might not have installed the nltk library in the active environment. Thus, with the help of the first command given below, check the active environment. Then, install the nltk library using the conda-forge channel, as mentioned in the second command.

conda info --envs
conda install -c conda-forge nltk

nameerror: name nltk is not defined in Spyder

You need to check whether the updated version exists or not. The first step is to upgrade the nltk library using $ pip install –upgrade nltk. After this, import the library and download the package. If you aren’t sure which model you will require, you can check out the popular ones. The second command depicts the same.

import nltk

nameerror: name nltk is not defined in pycharm

You may have another file named nltk.py on your system. It may be on the same path as well. Due to this reason, you might be getting the nameerror: name nltk is not defined error in pycharm. It is better to check all your Python files and rename them. Once this is done, consider re-installing your library.

Other than this, check the version of Python using the python –version and see if your installed version is compatible with the nltk module. Also, if you installed python 3 on your system, check its version using the python3 –version command.

Another way to resolve this error is to use pip install –allow-all-external –allow-unverified ntlk nltk by bypassing the security checks. To prevent it from overwriting any installation that already exists, you can force reinstall the nltk library with the help of pip install –upgrade –force-reinstall –allow-all-external –allow-unverified ntlk nltk command.

nameerror: name nltk is not defined in eclipse

The nameerror: name nltk is not defined. The error occurs in Eclipse when the download of the library doesn’t happen smoothly. Also, you might not have imported nltk. Thus, you need to import it first using import nltk and then download a package you want with nltk.download() command. This will open an installation window. Under MODELS, select the package/ dataset you wish to download, and you are good to go!

Otherwise, you can explicitly mention the package name that you wish to download using nltk.download(‘package_name’) command. Explicitly mentioning the package name is equally important. this is because, at times, nltk doesn’t recognize all the packages that have been downloaded due to its enormous size.

nameerror: name nltk is not defined in thonny

In order to resolve the nameerror: name nltk is not defined in thonny, you can use upip and specify the name of the package you want to install. Also, you can specify the path to your package.

import upip
upip.install("NAME OF PACKAGE")
upip.install("NAME OF PACKAGE", "c:\full\path\to\package")

Other tips while working with the nltk module

You can check these tips related to the nltk module while undergoing natural language processing (NLP) tasks. These can also help you combat the nameerror: name nltk is not defined error.

  • It is better to work with the full names of modules in nltk. Otherwise, it may lead to an error.
  • Exception handling will help you avoid minute errors.
  • NLTK documentation for all modules and functions is resourceful. You should go through them for more information.


I have already imported the nltk module, but I am still getting the error. Why?

If the import statement is written inside a function block, the scope is limited. Thus, you should import the nltk module before writing your code implementation.


This article unravels the complexities of the “nameerror: name nltk is not defined” error. It explains the reasons for the error and how you can get rid of this error.

Notify of
Inline Feedbacks
View all comments