Verify that the Python interpreter executing your script matches the exact environment where the package is installed (python -m pip install <package>), confirm your virtual environment is activated, and check sys.path for working-directory or file-shadowing issues—because ImportError: No module named ... (or ModuleNotFoundError in Python 3.6+) occurs whenever Python cannot locate the target module in any directory listed in sys.path.
Basic Diagnostic & Fix Workflow
# 1. Always install packages using the exact Python binary you run scripts with
python -m pip install <package_name>
# 2. Verify which Python interpreter is active and where its site-packages live
python -c "import sys; print(sys.executable); print(sys.path)"
# 3. Confirm the package is installed in that specific environment
python -m pip show <package_name>
Common Root Causes & Targeted Fixes
- 1. Mismatched
pythonandpipexecutables (multiple Python versions): Runningpip install requestsmay install into Python 3.11’ssite-packages, whilepython app.pyruns Python 3.12. Always pair them viapython -m pip install <package>(orpy -3.12 -m pip install <package>on Windows) so both commands target the exact same interpreter. - 2. Virtual environment not activated (or wrong interpreter selected in IDE): Installing a package inside
.venvand running your script globally—or vice versa—triggersModuleNotFoundError.- Terminal: Activate the virtual environment first (
source .venv/bin/activateon Linux/macOS, or.venv\Scripts\Activate.ps1on Windows PowerShell). - VS Code / PyCharm: Open the Command Palette (
Ctrl+Shift+P/Cmd+Shift+P) → Python: Select Interpreter → choose the interpreter inside your project’s.venvfolder.
- Terminal: Activate the virtual environment first (
- 3. PyPI distribution name differs from the
importname: Installing a package succeeds, yet importing it fails because the module name on disk differs from the PyPI package name (for example,pip install beautifulsoup4is imported asfrom bs4 import BeautifulSoup, andpip install scikit-learnis imported asimport sklearn). - 4. Local file or folder shadowing a library name: Naming your own script
requests.py,email.py,json.py, orpandas.pyplaces it first insys.path[0], causing Python to import your local file instead of the installed library. Rename your local file and delete any stale__pycache__folder. - 5. Running a script inside a package subdirectory (
sys.pathroot issue): If you have a project structure likeproject/src/utils.pyandproject/tests/test_app.py, runningpython tests/test_app.pysetssys.path[0]toproject/tests/, makingfrom src import utilsfail. Run scripts as modules from the project root instead:# Run from the root 'project/' directory using -m python -m tests.test_app
Root Cause & Solution Reference
| Root Cause | Diagnostic Symptom | Solution |
|---|---|---|
pip / python interpreter mismatch |
pip list shows the package, but python -m pip list does not |
Run python -m pip install <package> to install into the active interpreter. |
| Inactive virtual environment | sys.executable points to system Python instead of .venv |
Activate .venv in your shell or select it in your IDE’s interpreter picker. |
| Local script name collision | module.__file__ points to your project directory instead of site-packages |
Rename your local .py file so it does not shadow a built-in or third-party module. |
| Relative / parent package import failure | Importing a sibling or parent folder fails when running a nested script | Run from the project root with python -m package.module or install the project in editable mode (pip install -e .). |
| Different import vs. package name | pip install <name> succeeds, import <name> fails |
Check the library’s documentation for its actual top-level module name (e.g., Pillow → PIL, PyYAML → yaml). |
You must log in or register to comment.
