ModuleNotFoundError: No module named 'yaml'

Posted in Python by Dirk - last update: Feb 05, 2024

Python raises the ModuleNotFoundError: No module named 'yaml when it is unable to find the yaml library. The most likely cause is that you didn’t install yaml in the environment where you are running your code. Quick fix: install yaml using: the pip install yaml command

Possible causes and solutions

PyYAML is a YAML parser and emitter for Python. It supports both YAML 1.1 and 1.2 specifications. PyYAML can be used to load YAML data into Python objects (deserialization) and to convert Python objects into YAML (serialization).

Sample code:

import yaml

with open("example.yaml", "r") as file:
    data = yaml.load(file, Loader=yaml.FullLoader)


And when trying to run the code you get the ’no module named error':

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

Luckily, it is easy to fix. Below you can find the most common causes and how to fix them:

‘yaml’ Library is not installed

In your code you are importing the ‘yaml’ module

import yaml 

which generates the ModuleNotFoundError: No module named 'yaml’. The the most common cause for this error: the environment where you are running your code doesn’t have the yaml library installed.

You can use the following command in the terminal or command prompt to check if the yaml library is installed:

pip show yaml

If yaml is not installed you will get this result:

WARNING: Package(s) not found: yaml


pip install pyyaml

‘yaml’ Module installed in the Wrong Directory

It is possible that you did install the yaml module, but that the directory where you installed it is not in the Python path.

You can check the Python path in a few different ways:

1. Using sys module in Python:

Open a Python script or the Python interpreter and run the following code:

import sys

This will print a list of directories that make up the Python path.

2. Using Command Line

Open a command prompt or terminal and run:

  • Windows
  • Unix/Linux/macOS

If the PYTHONPATH environment variable is set, it will display the directories in the Python path.

Solution: Move the yaml module to a directory that is included in the Python path or add the yaml module’s directory to the sys.path using sys.path.append().

‘yaml’ not installed in Virtual Environment

Even if you have yaml installed in on your computer, if you are using a virtual environment you cannot access it from this environment.

If you are using virtual environments, ensure that the virtual environment is activated, and that the yaml modules is installed within this environment.

Installation is simple:

pip install pyyaml

File Naming Conflicts:

It is not really likely - but if you happen to have a file in your environment and you also installed the standard yaml library it will cause a conflict. Rename your script/module to avoid conflicts with existing module names.

Other articles