ModuleNotFoundError
builtins.ModuleNotFoundError
Stack trace
Traceback (most recent call last):
File "app.py", line 1, in <module>
import mcp
ModuleNotFoundError: No module named 'mcp' Why it happens
This error occurs when Python cannot find the 'mcp' module in the current environment. Usually, this means the package is not installed or the environment is misconfigured. It can also happen if the package is installed in a different Python environment than the one running the code.
Detection
Check for ModuleNotFoundError exceptions during import statements and verify if 'mcp' is installed by running 'pip show mcp' or 'pip list'.
Causes & fixes
The 'mcp' package is not installed in the current Python environment.
Run 'pip install mcp' in the active environment to install the package.
The code is running in a different Python environment than where 'mcp' was installed.
Ensure you activate the correct virtual environment or use the same Python interpreter where 'mcp' is installed.
Using a system Python without user site packages and 'mcp' installed only for the user.
Install 'mcp' globally or run Python with the '--user' flag or configure PYTHONPATH to include user site packages.
Code: broken vs fixed
import mcp # This line raises ModuleNotFoundError if 'mcp' is not installed import os
import mcp # Fixed by installing 'mcp' package via pip
# Example usage
print('MCP module imported successfully') Workaround
If you cannot install 'mcp' immediately, mock the module in your code using 'sys.modules' to avoid import errors temporarily.
Prevention
Use virtual environments consistently and document dependencies in requirements.txt or pyproject.toml to ensure all packages like 'mcp' are installed before running code.