Just like you use JSON file for writing the configuration of the project. In this same way, nowadays YAML files are used for creating a configuration file. To use it in python you have to install yaml package. But most of the developers are unable to install it and get the error modulenotfounderror: no module named ‘yaml’.
In this entire tutorial, you will know how to solve the modulenotfounderror: no module named ‘yaml’ error in a simple way.
What is ModuleNotFoundError in Python?
Most programmers face the ModuleNotFoundError when you are importing a module that is not installed in your system. Therefore you have to install the package in your system before using that package.
The root cause of modulenotfounderror: no module named ‘yaml’ error
The root cause of the no module named ‘yaml’ error is not proper installation of the python yaml package. For creating and using YAML file in python you have to install pyaml python package.
You will get the no module named ‘yaml’ error when you will import yaml.
Execute the below code and you will get the error.
You can see there is a redline below the yaml word. The python interpreter is telling you that the YAML package is not install in your system.
Solution of modulenotfounderror: no module named ‘yaml’
The solution for this error is very simple. YAML requires pyaml package. You have install it in your system. But before that, you have to check the python version of your system.
To check that open your terminal or command prompt and type the below bash command.
If the version of your python is 3.xx then use the pip3 command. And if it is python 2. xx then use the pip command.
For python 3. xx
pip install pyaml
For python 2. xx
pip install pyaml
The version of my python is 3. xx so I will use the pip3 command.
Now, if you import the YAML file you will get the no module named ‘yaml’ error.
The error modulenotfounderror: no module named ‘yaml’, you will mostly get when you do not have install pyaml package in your system. You can also get the error if the path for the package is not set in your environment variable.
The above method mostly works and will solve the modulenotfounderror: no module named ‘yaml’ error easily.