How to Solve "ModuleNotFoundError: No module named 'yaml'" in Python
The ModuleNotFoundError: No module named 'yaml'
error in Python means you're trying to use the YAML library (PyYAML), but it's not installed in your current Python environment.
This guide provides a step-by-step guide to install PyYAML
and fix this common error, covering different installation methods and troubleshooting tips.
Understanding the Error: yaml
vs. PyYAML
It's important to distinguish between the package name (used for installation) and the module name (used for importing):
PyYAML
: This is the package name you use withpip install
.yaml
: This is the module name you use in your Python code (e.g.,import yaml
).
Installing PyYAML
with pip
(Recommended)
The standard way to install PyYAML is with pip
:
Basic Installation
Open your terminal (or command prompt/PowerShell on Windows) and run:
pip install PyYAML
Or, if you're sure you're using Python 3:
pip3 install PyYAML
Using python -m pip
(If pip
Isn't in Your PATH)
If the pip
command doesn't work, try:
python -m pip install PyYAML # Use with your system's default Python
python3 -m pip install PyYAML # Use with your system's Python 3
- The command
python -m pip
uses the pip that is associated with the specific Python installation.
Windows-Specific Considerations (py
launcher)
On Windows, the py
launcher is often helpful:
py -m pip install PyYAML
- The command
py
will use the latest Python version.
Permissions Issues (sudo
or --user
)
On Linux/macOS, you might need sudo
for system-wide installation (but virtual environments are strongly preferred):
sudo pip3 install PyYAML # System-wide installation
sudo
runs the command with root privileges. Alternatively, install into your user's site-packages:
pip install PyYAML --user # Install for the current user only
- The
--user
argument installs the package to the current user's directory.
Using Virtual Environments (Best Practice)
Always use virtual environments for Python projects. This isolates dependencies and avoids conflicts.
Creating a Virtual Environment
python3 -m venv venv # Create an environment named "venv"
# OR: python -m venv venv
# OR: py -m venv venv (Windows)
Activating the Environment
-
macOS / Linux:
source venv/bin/activate
-
Windows (Command Prompt):
venv\Scripts\activate.bat
-
Windows (PowerShell):
venv\Scripts\Activate.ps1
You might also have to change the execution policy to activate virtual environment on PowerShell:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
After activation, your prompt will usually show the environment name (e.g., (venv)
). pip install
now installs only into this environment.
Installing PyYAML
Inside the Environment
With the virtual environment activated:
pip install PyYAML # Installs into the active environment
Troubleshooting
If you're still getting ModuleNotFoundError
after installing:
- IDE Configuration (VS Code, PyCharm): Your IDE might use a different interpreter.
- VS Code:
Ctrl+Shift+P
(orCmd+Shift+P
on macOS), "Python: Select Interpreter", choose your virtual environment. - PyCharm:
File > Settings > Project > Python Interpreter
(orPyCharm > Preferences > Project > Python Interpreter
on macOS), select your environment.
- VS Code:
- Conflicting Installations/Multiple Python Versions: If you have multiple Python versions without virtual environments, you may have installed
PyYAML
for the wrong one. Always use virtual environments. - Incorrect Filenames/Variable Names: Never name your Python files
yaml.py
. This conflicts with the installed library. Avoid variable names likeyaml
as well. - Restarting Your IDE/Kernel: Sometimes a restart is needed, especially in Jupyter.
- Reinstalling PyYAML:
pip uninstall pyyaml
pip install pyyaml
Using PyYAML
(Example)
Once installed, you can use PyYAML
to load and dump YAML data:
import yaml
data = {'name': 'John Doe', 'age': 30, 'city': 'New York'}
# Dump (serialize) to YAML
yaml_string = yaml.dump(data)
print(yaml_string)
# Load (deserialize) from YAML
loaded_data = yaml.safe_load(yaml_string) # Use safe_load for security
print(loaded_data)
Installing PyYAML
in Specific Environments
Anaconda
conda install -c conda-forge pyyaml
Jupyter Notebook
Within a notebook cell:
!pip install PyYAML
- The
!
runs the command as shell command.
Conclusion
The ModuleNotFoundError: No module named 'yaml'
error is resolved by correctly installing the PyYAML
package, preferably within a virtual environment.
- This guide provides complete instructions, troubleshooting steps, and an example of using the library.
- Always use virtual environments, and double-check your IDE settings if you encounter problems.