How to Solve "ModuleNotFoundError: No module named 'seaborn'" in Python
The error ModuleNotFoundError: No module named 'seaborn'
in Python indicates that the seaborn
data visualization library is not installed in your current Python environment.
This guide provides comprehensive solutions, including installation instructions for various environments and troubleshooting steps to resolve common issues.
Understanding the Error
The ModuleNotFoundError
means Python's import
system can not locate the seaborn
package. This nearly always means one of two things:
- Seaborn is not installed: You haven't installed the library.
- Wrong environment: You installed
seaborn
in a different Python environment than the one your script or IDE is currently using.
Basic Installation with pip
The standard way to install seaborn
is using pip
, Python's package installer. Open a terminal (Command Prompt/PowerShell on Windows, Terminal on macOS/Linux) and run one of the following commands:
pip install seaborn # Usually for Python 2, or in a virtual environment
pip3 install seaborn # Usually for Python 3 systems
python -m pip install seaborn # If 'pip' isn't directly in your PATH
python3 -m pip install seaborn # Python 3, if 'pip3' isn't in your PATH
py -m pip install seaborn # Windows 'py' launcher (selects appropriate Python)
The python -m pip
form (or its variants) is generally the most reliable because it explicitly uses the Python interpreter you intend.
Troubleshooting
If you still get the ModuleNotFoundError
after installation, follow these troubleshooting steps:
Virtual Environments
Are you using a virtual environment? If so, you must activate it before installing packages and before running your script.
-
Create (if needed):
python3 -m venv venv # Recommended: Use the built-in 'venv'
# OR (if the above fails)
python -m venv venv
# OR (Windows)
py -m venv venv -
Activate:
-
Linux/macOS (bash/zsh):
source venv/bin/activate
-
Windows (Command Prompt):
venv\Scripts\activate.bat
-
Windows (PowerShell):
venv\Scripts\Activate.ps1
If you see an "execution of scripts is disabled" error in PowerShell, run this command once (as administrator if needed) and then try activating again:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
-
-
Install inside the activated environment:
pip install seaborn