Nameerror: name ‘nltk’ is not defined ( Solved )

Nameerror name 'nltk' is not defined featured image

Are you getting the NameError: name ‘nltk’ is not defined error?  If yes then in this post you will know how to solve the Nameerror: name ‘nltk’ is not defined error easily.

What is NLTK?

NLTK stands for Natural Language Toolkit. It is a library that allows you to do natural language processing tasks like tokenization, stemming, and tagging. This library is mostly used by developers to analyze and manipulate textual data like text preprocessing, extraction, etc.

NLTK official website
NLTK official website

Why does the Nameerror: name ‘nltk’ is not defined occur?

There may be many reasons for getting this nameerror. You will know most of the general causes of it.

Cause 1: Missing NLTK Installation

The first cause for getting this nameerror is that the NLTK module must not be installed in your system. If you are importing the module then the Python interpreter will give you name ‘nltk’ is not defined error. The code will not run until the problem will be solved.

You will get the error when you import the module.

import nltk


Nameerror: name 'nltk' is not defined

Cause 2: Wrong import statement

When you use the nltk function in code and if you have not properly imported the nltk module ( import nltk )then you will get the error.

Cause 3: Environment Variables

You must have not set the environment variables for NLTK module and it may have given you an error.

The solution of name ‘nltk’ is not defined Error

Now let’s know all the solutions for this error.

Solution 1: Install NLTK module

The first solution is to install the NLTK module if the module is not installed in the system. Open your terminal and use the below command to install it.

pip install nltk

Soltuion 2: Correct Import statment

The second solution is to properly import the NLTK module. Most of the users get the error due to typo mismatched. So before using the NLTK functions import using the correct name like below.

import nltk

Solution 3: Set the Environment Variable

Verify that you have properly set the environment variable or not. If not then try to set the environment variable with the correct path of the NLTK installation folder.

Solution 4: Restart the kernel or Interpreter

If all the above solutions do not work then simply restart the Python kernel or interpreter to resolve the error. It will refresh all the installed packages in your Python environment.


Its a time-consuming task for many coders if they are unable to find the solutions to the NameError: name ‘nltk’ is not defined error. In this post, you have learned the most possible causes of the error and how to solve it properly. In most cases, you will get the error when NLTK is not installed or there will be typos mismatched.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.


Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner