Modulenotfounderror: no module named ‘yaml’ occurs when pyyaml module is not installed or misconfigured in the current running python interpreter. Since we maintain multiple python versions altogether. Hence installing any required python module is not sufficient. We need to make sure we are installing the package at the right location as well. Suppose you are invoking any python script from any virtual environment but you installed the package globally. It will not work in that scenario and throw a similar kind of error.
Well, we will discuss all these aspects in this article in detail. So let’s start the journey.
Since this error is related to pyyaml package hence the best solution is to install or reinstall it.
using pip package manager with a single command, we can install any python package which is uploaded in PyPI hub.
pip install pyyaml
pip3 install pyyaml
1. If you want to downgrade or upgrade pyyaml package because of any incompatibility. You can use the same command with the version as well. Please refer to the below example for a better understanding.
pip install PyYAML==5.4.1
Here is the version. You can opt for any of the versions on the basis of Pyyaml release history.
2. In case while running the above commands, you are facing an access right issue, add sudo before the command. It actually provides admin access. Please check the below command for reference.
sudo pip3 install pyyaml
3. Mostly when we install python, it automatically sets the path for pip but if pip path is not properly configured in the path variable try the below command.
python3 -m pip install pyyaml
It’s the default package manager with Anaconda distribution. It works quite similarly to pip. Here is the command for installation pyyaml conda.
conda install -c anaconda pyyaml
Every binary, executive, or installable file gets generated with source code. This is the most basic way to install or run any python package. The only downside is complexity. It is a bit more complex than above mention ways. Here you need to first download the source code from github. After that, you need to run the setup.py file. For more detailed documentation you will get into the PYYAML github.
python setup.py install
See it is very important to install the package at the required python interpreter. Especially If you are using a virtual environment then you need to first activate the environment and install the package.
python3 -m venv venv # creating virtual env
source venv/bin/activate #activation ( Unix and Mac Family OS)
pip install pyyaml
Only command activation will change for windows OS. Here is the required change.
venv\Scripts\activate.bat
Also, check out the generic reference to fix this error.
Thanks
Data Science Learner Team