PyGuide

Learn Python with practical tutorials and code examples

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

If you're getting a ModuleNotFoundError in Python even though you've clearly installed the package with pip, you're not alone. This is one of the most common and frustrating issues Python developers face. Let's explore the main causes and their solutions.

Quick Diagnosis: Check Your Environment #

The most common reason for this error is that pip installed the package in a different Python environment than the one you're running your code in.

Check Which Python You're Using #

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

This will show you exactly which Python interpreter is running your code and where it's looking for packages.

Common Causes and Solutions #

1. Multiple Python Installations #

Problem: You have multiple Python versions (system Python, Homebrew Python, Anaconda, etc.) and pip installed the package for a different Python than you're using.

Solution: Use the specific Python version's pip:

# Instead of just 'pip install package_name'
python3.9 -m pip install package_name
# or
/usr/local/bin/python3 -m pip install package_name

2. Virtual Environment Issues #

Problem: You installed the package globally but are running code inside a virtual environment (or vice versa).

Solution: Activate your virtual environment and install there:

# Activate your virtual environment
source venv/bin/activate  # Linux/Mac
# or
venv\Scripts\activate     # Windows

# Then install
pip install package_name

3. User vs System Installation #

Problem: The package was installed with --user flag but your Python isn't checking the user site-packages.

Solution: Install without --user or check if user site-packages is enabled:

import site
print(site.USER_SITE)
print(site.ENABLE_USER_SITE)

4. Package Name Mismatch #

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

Examples:

  • Install: pip install pillow → Import: import PIL
  • Install: pip install beautifulsoup4 → Import: import bs4
  • Install: pip install python-dateutil → Import: import dateutil

Solution: Check the package documentation for the correct import name.

Step-by-Step Debugging Process #

Step 1: Verify Installation Location #

pip show package_name

This shows where the package is installed.

Step 2: Check Python Path #

import sys
for path in sys.path:
    print(path)

Ensure the installation location appears in this list.

Step 3: Force Reinstall #

pip uninstall package_name
pip install package_name

Step 4: Use Python Module Flag #

python -m pip install package_name

This ensures pip uses the same Python interpreter you're running.

IDE-Specific Solutions #

VS Code #

  • Ensure you've selected the correct Python interpreter (Ctrl+Shift+P → "Python: Select Interpreter")
  • Check that the interpreter matches your virtual environment

PyCharm #

  • Go to Settings → Project → Python Interpreter
  • Verify the correct interpreter is selected
  • Install packages through the IDE's package manager

Prevention Tips #

  1. Always use virtual environments for projects
  2. Use python -m pip install instead of just pip install
  3. Verify your environment before installing packages
  4. Document your Python setup for each project

When All Else Fails #

If you're still having issues, try creating a fresh virtual environment:

python -m venv fresh_env
source fresh_env/bin/activate  # Linux/Mac
fresh_env\Scripts\activate     # Windows
pip install package_name
python your_script.py

Remember: the key to solving "how to fix python import modulenotfounderror when package is clearly installed pip" is understanding that Python environments can be tricky, and the package might be installed in a different environment than where your code is running.