Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

ImportError in Python — No module named Fix (2026) | Tutorials Logic

What is ImportError?

An ImportError occurs in Python when an import statement fails to find or load a module. The most common subclass is ModuleNotFoundError (Python 3.6+), which specifically means the module does not exist in the current environment. This error typically happens when a package is not installed, the module name is misspelled, or the Python environment is incorrect.

Common Causes

  • Package not installed in the current Python environment
  • Typo or wrong capitalization in the module name
  • Circular imports between two modules that import each other
  • Running the script in the wrong Python virtual environment
  • Importing a name that does not exist in the module

Quick Fix (TL;DR)

Quick Solution
# ❌ Problem
import requests  # ImportError: No module named 'requests'

# ✅ Solution: Install the package first
# pip install requests

# Then import works:
import requests
response = requests.get("https://api.example.com/data")

Common Scenarios & Solutions

Scenario 1: Package Not Installed

Third-party packages like requests, numpy, or pandas are not part of Python's standard library. You must install them with pip before importing them.

Problem
import numpy as np   # ModuleNotFoundError: No module named 'numpy'
import pandas as pd  # ModuleNotFoundError: No module named 'pandas'
Solution
# ✅ Install via pip in your terminal
# pip install numpy pandas

# ✅ Or install from requirements.txt
# pip install -r requirements.txt

# ✅ Verify installation
# pip show numpy

import numpy as np   # Works after installation
import pandas as pd  # Works after installation

Scenario 2: Wrong Module Name

Module names are case-sensitive and must be spelled exactly. The package name you install with pip is sometimes different from the module name you import (e.g., pip install Pillow but import PIL).

Problem
import Requests    # ModuleNotFoundError (should be lowercase 'requests')
import sklearn     # ModuleNotFoundError (pip install scikit-learn, import sklearn)
import cv2         # ModuleNotFoundError (pip install opencv-python, import cv2)
Solution
import requests    # ✅ lowercase

# pip install scikit-learn
import sklearn     # ✅ import name differs from pip name

# pip install opencv-python
import cv2         # ✅ import name differs from pip name

# pip install Pillow
from PIL import Image  # ✅ import PIL, not Pillow

Scenario 3: Circular Import

A circular import occurs when module A imports module B, and module B also imports module A. Python partially loads modules, so the second import may fail to find names that haven't been defined yet.

Problem
# module_a.py
from module_b import func_b  # ImportError: cannot import name 'func_b'

def func_a():
    return "A"

# module_b.py
from module_a import func_a  # Circular import!

def func_b():
    return func_a() + "B"
Solution
# ✅ Solution 1: Move shared code to a third module (module_common.py)
# module_common.py
def func_a():
    return "A"

# module_b.py
from module_common import func_a
def func_b():
    return func_a() + "B"

# ✅ Solution 2: Import inside the function (lazy import)
# module_a.py
def func_a():
    from module_b import func_b  # Import only when needed
    return func_b()

Scenario 4: Wrong Python Environment

When using virtual environments, a package installed in one environment is not available in another. This is a common issue when switching between projects or when an IDE uses a different Python interpreter than the terminal.

Problem
# Installed requests in venv1, but running script with venv2 or system Python
# pip install requests  (in venv1)
# python script.py      (using system Python — no requests!)
import requests  # ModuleNotFoundError
Solution
# ✅ Activate the correct virtual environment first
# Windows: venv\Scripts\activate
# macOS/Linux: source venv/bin/activate

# ✅ Verify which Python you're using
import sys
print(sys.executable)  # Shows the Python interpreter path

# ✅ Install in the active environment
# python -m pip install requests  (uses the active Python's pip)

Best Practices

  • Use virtual environments - Always create a venv per project to isolate dependencies and avoid conflicts.
  • Maintain a requirements.txt - Run pip freeze > requirements.txt to document all dependencies for reproducibility.
  • Use python -m pip install - This ensures pip installs into the same Python that will run your script.
  • Check pip vs import names - The pip package name and the import name are sometimes different (e.g., pip install Pillowimport PIL).
  • Avoid circular imports - Restructure code to extract shared functionality into a separate module rather than having modules import each other.
  • Use optional imports gracefully - Wrap optional imports in try/except ImportError to provide a fallback when a package is not available.
  • Use pyproject.toml or setup.py - For packages, declare dependencies formally so they are installed automatically.

Related Errors

Key Takeaways
  • ImportError (and its subclass ModuleNotFoundError) occurs when Python cannot find or load a module.
  • Third-party packages must be installed with pip before they can be imported.
  • The pip install name and the import name are sometimes different (e.g., Pillow vs PIL).
  • Always use virtual environments to isolate project dependencies.
  • Circular imports can be resolved by extracting shared code into a third module.
  • Use python -m pip install to ensure packages are installed in the correct environment.

Frequently Asked Questions


Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.