How to Fix ModuleNotFoundError: No module named in Python With Examples

 

 What Is ModuleNotFoundError?

ModuleNotFoundError is a common Python error that occurs when you try to import a module that is not installed, not found in your environment, or misspelled.

Example:

import numpy  # Error if numpy is not installed

Output:

ModuleNotFoundError: No module named 'numpy'

How to Fix ModuleNotFoundError

1.  Install the Missing Module

Use pip to install the module:

pip install numpy

For Python 3 specifically:

pip3 install numpy

If you're using a virtual environment, activate it first.

2.  Check for Typos

Make sure the module name is spelled correctly.

Wrong:

import numppy  # Typo!

Right:

import numpy

3.  Check Your Environment

You may have installed the module in a different Python environment. To check where Python is looking for modules:

import sys
print(sys.path)

Also, check which Python and pip versions you're using:

python --version
pip --version

Make sure you're using the same interpreter where the module is installed.

4.  Use a Virtual Environment (Optional but Recommended)

Create and activate a virtual environment to avoid conflicts:

python -m venv venv
source venv/bin/activate  # On Linux/macOS
venv\Scripts\activate     # On Windows

Then install your modules:

pip install <module_name>

5.  Using Jupyter or IPython?

If you're using Jupyter Notebook, run this in a cell:

!pip install numpy

Or if that doesn't work, try installing via the notebook environment's kernel:

import sys
!{sys.executable} -m pip install numpy

 Summary Table

Problem Solution
Module not installed pip install module_name
Wrong Python environment Use correct virtual environment
Typo in module name Double-check spelling
Using Jupyter or special environment Use !pip install inside notebook
Installed globally but not in venv Activate virtualenv and install again

0 Comments:

Post a Comment