PyGuide

Learn Python with practical tutorials and code examples

How to Fix Python Import ModuleNotFoundError When Package is Clearly Installed with Pip

If you're encountering a python import module not found error despite package being installed with pip, you're not alone. This frustrating issue affects many Python developers and can have several root causes. Let's walk through the most effective solutions to resolve this common problem.

Quick Diagnostic Check #

Before diving into solutions, verify your installation with this simple check:

import sys
print(sys.executable)
print(sys.path)

Run this in your Python environment to see which Python interpreter and paths are being used.

Common Causes and Solutions #

1. Multiple Python Installations Conflict #

Problem: You have multiple Python versions, and pip installed the package to a different Python installation than the one you're using.

Solution:

# Check which Python pip is using
pip --version

# Check which Python you're running
python --version
which python

# Use python -m pip instead of just pip
python -m pip install package_name

2. Virtual Environment Issues #

Problem: Package installed globally, but you're working in a virtual environment (or vice versa).

Solution:

# Activate your virtual environment first
source venv/bin/activate  # On macOS/Linux
# or
venv\Scripts\activate     # On Windows

# Then install the package
pip install package_name

# Verify installation in the correct environment
pip list

3. Python Path Problems #

Problem: Python can't find the installed package due to path configuration issues.

Solution:

import sys
import os

# Add the site-packages directory manually
site_packages = "/path/to/your/site-packages"
if site_packages not in sys.path:
    sys.path.append(site_packages)

# Or find it dynamically
import site
print(site.getsitepackages())

4. Package Name vs Import Name Mismatch #

Problem: The pip package name differs from the import name.

Solution:

# Example: Install pillow, but import PIL
pip install pillow
# Import using the correct module name
from PIL import Image  # Not "import pillow"

Common mismatches:

  • pip install pillowimport PIL
  • pip install beautifulsoup4import bs4
  • pip install opencv-pythonimport cv2

5. Corrupted Installation #

Problem: The package installation was corrupted or incomplete.

Solution:

# Uninstall and reinstall the package
pip uninstall package_name
pip cache purge
pip install package_name

# Force reinstall
pip install --force-reinstall package_name

Advanced Troubleshooting Steps #

Check Package Installation Location #

# Find where the package is installed
pip show package_name

# List all installed packages
pip list

# Check if package files exist
python -c "import package_name; print(package_name.__file__)"

IDE-Specific Issues #

PyCharm/VSCode Problems:

  1. Check interpreter settings in your IDE
  2. Refresh interpreter cache
  3. Ensure IDE is using the correct virtual environment

Jupyter Notebook Issues:

# Install package in Jupyter's kernel
!pip install package_name

# Or use sys.executable to ensure correct Python
import sys
!{sys.executable} -m pip install package_name

System-Level Solutions #

On macOS:

# Use homebrew Python instead of system Python
brew install python
/opt/homebrew/bin/python3 -m pip install package_name

On Ubuntu/Debian:

# Install python3-pip if missing
sudo apt update
sudo apt install python3-pip

# Use python3 explicitly
python3 -m pip install package_name

Prevention Best Practices #

1. Always Use Virtual Environments #

# Create virtual environment
python -m venv myproject_env

# Activate it
source myproject_env/bin/activate  # macOS/Linux
myproject_env\Scripts\activate     # Windows

# Install packages
pip install -r requirements.txt

2. Use Explicit Python Commands #

# Instead of just "pip install"
python -m pip install package_name

# Instead of just "python"
python3 script.py

3. Document Your Environment #

# Save current package versions
pip freeze > requirements.txt

# Install from requirements in new environment
pip install -r requirements.txt

Common Mistakes to Avoid #

  • Don't mix conda and pip without understanding the implications
  • Don't install packages globally when working on projects
  • Don't assume package name equals import name
  • Don't skip virtual environment activation
  • Don't use sudo pip install on Unix systems

When All Else Fails #

If none of these solutions work:

  1. Create a fresh virtual environment
  2. Reinstall Python if using system Python
  3. Check for typos in package and import names
  4. Consult package documentation for specific installation instructions
  5. Check GitHub issues for known installation problems

Summary #

The python import module not found error despite package being installed typically stems from environment conflicts, path issues, or installation problems. Start with checking your Python and pip versions, ensure you're in the correct virtual environment, and verify the package installation. Most import issues resolve with proper environment management and explicit Python/pip commands.

Remember: consistency in Python environment management prevents most import-related headaches. Always activate your virtual environment before installing packages, and use python -m pip for installations to ensure you're using the correct Python interpreter.