ModuleNotFoundError: No module named ‘yaml’ in Python

To interact with the scripting language, Python provides a “yaml” module that is used for writing configuration. To use this module in a Python program, you need to install it in a Python environment. The module not found error occurs when you access this module without installing it in Python.

This Python post will explain how to resolve the “No module named yaml” error in Linux and windows. The following reason and solutions are discussed in this article:

So, let’s get started with the first reason!

Reason: ‘yaml’ Module Not Installed in Python

The prominent reason which causes this error in Python is when we try to import the “yaml” module without being installed in Python. The below code shows “ModuleNotFoundError”:

The above output shows the “No module named yaml” error because the “yaml” module has not been installed in Python.

Solution: Install the ‘yaml’ Module in Python (For Windows Users)

To resolve this error, you need to install the Python “yaml” module using the pip package manager. If you don’t have a pip, you can install it by following this specific article.

Step 1: Open Command Prompt Terminal

After installing Python pip open the command prompt terminal by following the below snippet:

Step 2: Install the ‘yaml’ Module Using the pip

After opening cmd, type the below pip command and press enter to execute it:

> pip install pyyaml

The above snippet verified that the pip package manager successfully installed the “pyyaml” module in Python.

Sometimes, the permission error arises in the program after typing the above command. To handle this error, you can use the given below command to install the “pyyaml” module in Python:

> pip install pyyaml --user

In our case, the module is already installed, so the above command has been executed and displays the confirmation of the installed module.

Step 3: Verification of Installed “yaml” Module

After installing the module, we can now verify the installation of the “yaml” module using the following pip command:

> pip show pyyaml

The above snippet shows the name, version, and summary of the installed “yaml” module in Python.

Install the ‘yaml’ Module in Anaconda

To install the “yaml” module in anaconda; you can use the given below command:

> conda install -c conda-forge pyyaml

Install the ‘yaml’ Module in Jupyter NoteBook

To install the “yaml” module in Jupyter Notebook you can execute the below command:

> !pip install pyyaml

Solution: Install ‘yaml’ Module in Python (For Linux Users)

To resolve this “ModuleNotFoundError” in Linux you can also use the pip package manager. If you don’t have a pip package manager, follow this installation guide. You can follow this guide to install Python pip specifically on Ubuntu 22.04.

You can also run the given below command to install Python pip on Ubuntu:

$ sudo apt install python3-pip

After installing the pip package manager now type the following command to install Python “pyyaml” module:

$ pip install pyyaml

If the above command does not work appropriately, type the given below command:

$ sudo pip3 install PyYAML

There is another option to install the “yaml” module in Linux, i.e., by using Linux package manager:

$ sudo apt update -y
#install yaml Module
$ sudo apt install -y python-yaml

That’s how the ‘yaml’ error can be fixed in Python.

Conclusion

The “No module named yaml” error occurs in Python when a user tries to import the “yaml” module without installing it in Python. To resolve this error, you can use the “pip” command in Windows and Linux to install the “yaml” module. The installation of the “yaml” module in Ubuntu 22.04 can be done using the “apt” command. This article presented a detailed guide on how to install the “yaml” module in Python.