How to Resolve "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
Checking Your Python Version
Ensure you're installing into the correct Python version:
python --version # Or python3 --version, or py --version
If you have multiple Python versions, use the pip
associated with the correct version (e.g., pip3.9 install seaborn
for Python 3.9).
Verifying Installation
Confirm seaborn
is installed in the active environment:
pip show seaborn
# OR
python -m pip show seaborn
This should show package information, including the installation location. If it says "Package not found", you're either in the wrong environment, or the installation failed. Check the Location:
field in the output. It should point to your virtual environment's site-packages
(if using a virtual environment).
IDE Configuration (VS Code, PyCharm)
Your IDE must be configured to use the correct Python interpreter (the one where seaborn
is installed).
- VS Code: Use the
Python: Select Interpreter
command from the Command Palette (Ctrl+Shift+P
orCmd+Shift+P
). - PyCharm: Go to
File > Settings > Project > Python Interpreter
(orPyCharm > Preferences > Project > Python Interpreter
on macOS).
Naming Conflicts (Shadowing)
Make sure you don't have a file or directory named seaborn.py
(or seaborn
) in your project directory or anywhere on your PYTHONPATH
. This will "shadow" the installed seaborn
package. Rename your file if this is the case.
Reinstalling Seaborn
If you still experience problems you may try to uninstall and reinstall the package:
pip uninstall seaborn
pip install seaborn
Upgrading pip
It is also a good idea to upgrade pip
to its latest version before installing packages.
python -m pip install --upgrade pip
Installation in Specific Environments
Anaconda
conda install -c anaconda seaborn
- When using Anaconda it is important to use its package manager
conda
.
Jupyter Notebook
!pip install seaborn
- You must include the
!
symbol before thepip install
command to install the library.
Basic Seaborn Example
Once installed, you can import and use seaborn
:
import seaborn as sns
import matplotlib.pyplot as plt # Required for showing plots
sns.set_theme() # Apply default Seaborn theme (optional, but good practice)
# Load a sample dataset (comes with Seaborn)
tips = sns.load_dataset("tips")
# Create a relational plot
sns.relplot(
data=tips,
x="total_bill", y="tip", col="time",
hue="smoker", style="smoker", size="size",
)
plt.show() # Display the plot
- This uses the
relplot()
function from seaborn, and also requiresmatplotlib.pyplot
to be imported to display the plot.
Conclusion
The ModuleNotFoundError: No module named 'seaborn'
error is typically a straightforward installation issue.
This guide covered installation using pip
, troubleshooting steps (especially checking virtual environments and IDE configurations), and provided installation instructions for common environments like Anaconda and Jupyter.
By following these steps, you should be able to resolve the error and start using Seaborn for your data visualization needs.