How to solve ModuleNotFoundError: No module named 'yaml'

How to solve ModuleNotFoundError: No module named 'yaml'

The ModuleNotFoundError: No module named 'yaml' error appears when the program cannot import the YAML module. However, this tutorial will show you five solutions for solving the error.

Solution 1: Install YAML packages

90% of ModuleNotFoundError: No module named 'yaml' is because the Yaml packages are not installed. Therefore you need to install YAML:

Using PIP:

pip install PyYAML # Python 2
pip3 install PyYAML # Python 3

Using Anaconda:

conda install -c anaconda yaml

If the error is not fixed, move to solution 2.

Solution 2: Ensure you use Python <= 3

As you know, most of the python libraries no longer support python 2. Make sure you execute the program with Python 3.

By using the following commands, you can see your python version:

python -V
# OR
python3 -V

If Python 3 is not found on your device, Install Python on Windows, Mac, and Linux.

Solution 3: Ensure you create virtualenv with Python <= 3

Another reason for ModuleNotFoundError: No module named 'yaml' is creating the environment with Python 2 instead of Python 3. Use the following command to create your env with Python 3.

python3 -m venv myenv

Now, Install YAML packages using pip3.

Solution 5: re-install python-yaml and dependencies

The last solution is re-install  python-yaml and dependencies:

Remove Yaml:

$ sudo apt-get remove python3-yaml
#
$ sudo apt-get remove --auto-remove python3-yaml

Then Install Yaml again:

$ sudo apt-get purge python3-yaml
$ sudo apt-get purge --auto-remove python3-yaml

Conclusion

To summarize this article, we can say that there are many solutions to solve ModuleNotFoundError: No module named 'yaml'. We start with installing the yarml package, and we end with uninstalling python-YAML and dependencies.

For more about ModuleNotFoundError, Check out ModuleNotFoundError: No module named in Python.

Happy Coding ♥