Skip to main content

How to Resolve "ModuleNotFoundError: No module named 'nltk'" in Python

The error ModuleNotFoundError: No module named 'nltk' in Python means that the Natural Language Toolkit (NLTK) library is not installed in the Python environment you're currently using.

This guide provides a step-by-step guide to installing NLTK and resolving this common error, covering different installation methods and troubleshooting tips.

Understanding the Error​

The ModuleNotFoundError occurs when Python can't find a module you're trying to import. In this case, it's the nltk module. This almost always means NLTK is not installed, or it's installed in a different Python environment than the one you're currently using.

The standard way to install NLTK is using pip, Python's package installer.

Basic Installation​

Open your terminal (or command prompt on Windows) and run:

pip install nltk

If you have multiple Python versions or pip is associated with Python 2, use:

pip3 install nltk # For Python 3 (may be pip3.x on some systems)

Using python -m pip (If pip Isn't in Your PATH)​

If the pip command doesn't work, try:

python -m pip install nltk # Use with your system's default Python
python3 -m pip install nltk # Use with your system's Python 3
  • Using python -m pip ensures that you're using the pip associated with the Python interpreter you're running.

Windows-Specific Considerations (py launcher)​

On Windows, the py launcher is often the best way:

py -m pip install nltk

Permissions Issues (sudo or --user)​

On Linux/macOS, you might need sudo for a system-wide installation (but virtual environments are strongly preferred):

sudo pip3 install nltk # System-wide install. Use with caution.

Alternatively, install into your user's site-packages (no sudo needed):

pip install nltk --user # Install for the current user only
  • The --user argument installs the package only for the current user.

Always use virtual environments for Python projects. They isolate dependencies.

Creating a Virtual Environment​

python3 -m venv venv # Create an environment named "venv"
# OR: python -m venv venv
# OR: py -m venv venv (Windows)

Activating the Environment​

  • macOS / Linux:

    source venv/bin/activate
  • Windows (Command Prompt):

    venv\Scripts\activate.bat
  • Windows (PowerShell):

    venv\Scripts\Activate.ps1
    • You may also have to change the ExecutionPolicy:
      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

After activation, your prompt will usually show the environment name (e.g., (venv)).

Installing NLTK Inside the Environment​

With the virtual environment activated:

pip install nltk # Installs into the active environment

Troubleshooting​

If you've installed nltk but still get the ModuleNotFoundError:

  • IDE Configuration (VS Code, PyCharm): Your IDE might be using the wrong interpreter.
    • VS Code: Ctrl+Shift+P (or Cmd+Shift+P on macOS), "Python: Select Interpreter", choose your virtual environment.
    • PyCharm: File > Settings > Project > Python Interpreter (or PyCharm > Preferences > Project > Python Interpreter on macOS), select your environment.
  • Conflicting Installations/Multiple Python Versions: If you have multiple Python versions without virtual environments, you may have installed nltk for the wrong one. Always use virtual environments!
  • Incorrect Filenames/Variable Names: Never name your Python files nltk.py. This conflicts with the installed library. Avoid variable names like nltk as well.
  • Restarting Your IDE/Kernel: Sometimes (especially in Jupyter), you need to restart.
  • Reinstalling NLTK:
pip uninstall nltk
pip install nltk

Using NLTK (Example)​

import nltk

print(nltk.__version__) # Check the installed version

nltk.download('punkt') # Download required data (do this once)
nltk.download('averaged_perceptron_tagger')
sentence = """At eight o'clock on Thursday morning
Arthur didn't feel very good."""
tokens = nltk.word_tokenize(sentence)
print(tokens)
# ['At', 'eight', "o'clock", 'on', 'Thursday', 'morning',
# 'Arthur', 'did', "n't", 'feel', 'very', 'good', '.']
  • The first time you use NLTK, you'll likely need to download some data. The nltk.download('punkt') line downloads a required tokenizer model. You may need to download other data depending on what NLTK features you use. The NLTK documentation provides more details.

Installing NLTK in Specific Environments​

Anaconda​

conda install -c anaconda nltk

Jupyter Notebook​

Within a notebook cell:

!pip install nltk
  • The ! character allows you to run shell commands in Jupyter.

Conclusion​

The No module named 'nltk' error is resolved by installing the nltk package using pip, ideally within a virtual environment.

This guide provides a comprehensive, step-by-step guide to installation and troubleshooting, ensuring that users can quickly get NLTK up and running.