Last modified: Apr 20, 2026 By Alexander Williams

How to solve ModuleNotFoundError: No module named 'yaml'

If you are working with Python and suddenly see ModuleNotFoundError: No module named 'yaml', do not worry. This is one of the most common errors Python developers encounter. It simply means Python cannot find the yaml module in your environment.

In this article, you will learn exactly why this error happens and how to fix it step by step. We will cover all major environments, including virtual environments, Jupyter Notebook, Anaconda, and Docker.

What Is the yaml Module?

The yaml module is used to parse and write YAML data in Python. YAML stands for "YAML Ain't Markup Language." It is a human-readable data format used widely in configuration files, DevOps pipelines, and data serialization.

The most popular Python library for YAML is PyYAML. It provides the yaml.load(), yaml.dump(), and related functions to read and write YAML content.

PyYAML is not part of the Python standard library. That is why you need to install it manually before you can import it.

What Causes This Error?

This error appears when Python tries to import the yaml module but cannot locate it. Here is what the error typically looks like:


import yaml

Traceback (most recent call last):
  File "app.py", line 1, in <module>
    import yaml
ModuleNotFoundError: No module named 'yaml'

There are several common reasons this error occurs:

1. PyYAML is not installed — This is the most common cause. You have never installed the library in your environment.

2. Wrong Python environment — You installed PyYAML in a different Python version or virtual environment than the one you are currently using.

3. Virtual environment not activated — You installed the package globally but are running code inside a virtual environment where it is not available.

4. Typo in the package name — The import name is yaml but the package name is PyYAML. Confusing these two is a common mistake.

Fix 1: Install PyYAML Using pip

The fastest solution is to install PyYAML using pip. Open your terminal or command prompt and run:


pip install pyyaml

If you are using Python 3 and have multiple Python versions installed, use:


pip3 install pyyaml

After installation, verify it was installed successfully:


pip show pyyaml

Name: PyYAML
Version: 6.0.1
Summary: YAML parser and emitter for Python
Home-page: https://pyyaml.org/
Author: Kirill Simonov

Now try importing it again in your Python script:


import yaml

# Simple test to confirm yaml works
data = yaml.safe_load("name: John\nage: 30")
print(data)

{'name': 'John', 'age': 30}

If you see this output, the error is resolved.

Fix 2: Use the Correct Python Interpreter

Sometimes PyYAML is installed, but in the wrong Python environment. This is a very common issue when you have multiple Python versions on your machine.

To install PyYAML for a specific Python version, use:


python3.10 -m pip install pyyaml

Replace python3.10 with your actual Python version. You can check your version by running:


python --version

Using the -m pip approach ensures you install the package into the exact Python interpreter you are using to run your script.

Fix 3: Activate Your Virtual Environment

If you are using a virtual environment, you must activate it before installing any package. This is a very common mistake among beginners.

To create and activate a virtual environment on Linux or macOS:


# Create virtual environment
python -m venv myenv

# Activate it
source myenv/bin/activate

# Now install pyyaml inside the virtual environment
pip install pyyaml

On Windows, the activation command is slightly different:


# Create virtual environment
python -m venv myenv

# Activate it on Windows
myenv\Scripts\activate

# Install pyyaml
pip install pyyaml

Always make sure your virtual environment is activated before running your Python scripts.

Fix 4: Install PyYAML in Anaconda

If you are using the Anaconda distribution, pip may not always be the best option. Use the conda package manager instead:


conda install -c conda-forge pyyaml

Alternatively, you can still use pip inside a conda environment:


pip install pyyaml

Make sure you have activated the correct conda environment first:


conda activate myenv

Fix 5: Install PyYAML in Jupyter Notebook

If you are using Jupyter Notebook, the standard terminal installation may not affect the notebook kernel. Run the installation directly inside a notebook cell:


import sys

# Install pyyaml inside Jupyter Notebook
!{sys.executable} -m pip install pyyaml

Using sys.executable ensures that the package is installed into the same Python kernel that Jupyter is using. This avoids the common problem of installing into the wrong environment.

After installation, restart the kernel and try importing yaml again.

Fix 6: Install PyYAML in a Docker Container

If you are running your application inside a Docker container, you need to add PyYAML to your requirements.txt file or install it directly in your Dockerfile.

Add this line to your requirements.txt:


pyyaml

Or install it directly inside your Dockerfile:


RUN pip install pyyaml

Then rebuild your Docker image:


docker build -t myapp .

How to Use PyYAML Correctly

Once PyYAML is installed, here are some basic usage examples to get you started.

Reading a YAML file:


import yaml

# Open and read a YAML file safely
with open("config.yaml", "r") as file:
    config = yaml.safe_load(file)

print(config)

{'database': 'mydb', 'host': 'localhost', 'port': 5432}

Writing data to a YAML file:


import yaml

data = {
    "name": "Alice",
    "age": 25,
    "skills": ["Python", "Docker", "YAML"]
}

# Write Python dictionary to YAML file
with open("output.yaml", "w") as file:
    yaml.dump(data, file, default_flow_style=False)

print("YAML file created successfully!")

YAML file created successfully!

Always use yaml.safe_load() instead of yaml.load() when loading untrusted YAML data. The safe_load method prevents arbitrary code execution from malicious YAML content.

Avoid Common Mistakes

Here are a few mistakes developers often make when dealing with this error.

Wrong package name: The import name is yaml but the installation name is PyYAML. Never try to install it with pip install yaml — that will install a completely different and outdated package.


# WRONG - do not do this
pip install yaml

# CORRECT - always use this
pip install pyyaml

Installing globally while running in a virtual environment: If you install PyYAML outside your activated virtual environment, Python inside the virtual environment will still not find it.

Similar issues can happen with other third-party modules. For example, if you have seen ModuleNotFoundError: No module named 'keras' or ModuleNotFoundError: No module named 'pywt', the root cause and fix are often the same — the package is simply not installed in the active Python environment.

Verify Your Installation Environment

Before installing any package, it is always a good practice to verify which Python and pip you are using. Run these commands:


# Check which Python is active
which python

# Check which pip is active
which pip

# List installed packages
pip list | grep -i yaml

/usr/local/bin/python
/usr/local/bin/pip
PyYAML                    6.0.1

This helps you confirm that both Python and pip point to the same environment, and that PyYAML is actually installed there.

Conclusion

The ModuleNotFoundError: No module named 'yaml' error is easy to fix once you understand why it happens. In most cases, the solution is simply to install PyYAML using pip install pyyaml.

The key points to remember are:

Always install the package with the correct name pyyaml, not yaml. Always make sure you are installing into the right Python environment. Always activate your virtual environment before running your code.

By following the steps in this article, you should be able to resolve this error in any environment — whether you are using a standard Python installation, a virtual environment, Anaconda, Jupyter Notebook, or Docker.

If you continue to face issues, double-check your Python path and make sure your IDE is pointing to the correct interpreter. Once that is sorted, PyYAML will work without any problems.