Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

ModuleNotFoundError: No module named in Python

Verified FixPython 3.10+Python NativeSilo: environment

Quick Fix / Solution Rapide

This error occurs when importing an uninstalled library or when using the wrong Python interpreter. Install the package via python -m pip install <package_name>.

Root Cause Analysis

This error occurs when Python tries to load a library or module using the import statement, but the specified module name cannot be located in the current Python environment's sys.path.

Cause 1: Package Installed in a Different Python Interpreter

Developers often have multiple Python installations (system Python, Homebrew, pyenv, Anaconda, VS Code virtual environment). Running pip install requests may install the package into interpreter A while running python script.py executes interpreter B.

Cause 2: PyPI Package Name Differs from Import Name

Several popular packages use different names on PyPI vs in Python import statements. For example, pip install scikit-learn imports as import sklearn, pip install pillow imports as import PIL, and pip install pyyaml imports as import yaml.

Cause 3: Inactive Virtual Environment

Launching a script without first activating the virtual environment (source .venv/bin/activate or .venv\Scripts\activate) forces Python to fall back to global system packages.

Reproduction Code (MCVE)

Example: Bug Reproduction
import non_existent_package_xyz123

Solution 1: Install Using python -m pip to Guarantee Interpreter Alignment

Always run python -m pip install <package_name> rather than bare pip. This guarantees that pip installs packages into the exact interpreter executing Python commands.

Example: Recommended Solution
import sys
import subprocess

# Diagnostic: Verify active interpreter path
print(f'Active Python interpreter: {sys.executable}')
print('Install packages using: python -m pip install <package_name>')

Solution 2: Verify Package Import Name Mapping

Confirm the documented Python import name for third-party libraries on PyPI (e.g. pyyaml -> import yaml).

Example: Alternative Solution
import json
# Standard library modules require no pip installation
print(f'JSON module loaded successfully from: {json.__file__}')

Common Mistakes & Edge Cases

1. Contrasting ModuleNotFoundError vs ImportError

  • ModuleNotFoundError: The entire module or file cannot be found in sys.path (introduced in Python 3.6 as a subclass of ImportError).
  • ImportError: The module was found, but a specific class, function, or symbol inside the module does not exist (or circular imports prevent initialization).

2. Script Name Collisions

Naming your script email.py, random.py, or math.py causes import random to import your local file instead of the standard library, leading to mysterious missing attribute errors.

3. Inspecting sys.path

Print sys.path in your script to inspect all search directories Python uses to locate packages.

4. Note de Reproductibilité Environnementale

Le comportement des commandes système et des résolutions de paquets dépend fortement de votre système d'exploitation (Windows, macOS, Linux), de l'architecture processeur (x86_64 vs ARM64) et de la configuration des permissions locales. Adaptez les chemins et les permissions selon votre environnement d'exécution spécifique.