ImportError No module named ‘yaml’ in Python

ImportError: No module named ‘yaml’ is raised when Python has trouble loading the PyYAML module in the “import yaml” statement. This happens primarily when the module can’t be located.

In most cases, ModuleNotFoundError is raised instead of ImportError in Python 3.6+ (the former is a subclass of the latter). That means we will cover both of the following errors:

ModuleNotFoundError: No module named ‘yaml’, and

ImportError: No module named yaml

When you run into any of the above errors, the PyYAML package is not installed or installed in a path not accessible by the Python interpreter. Effectively, the error can be fixed by installing or reinstalling the package.

Solution A: Installing the PyYAML package

Here are three methods to install the PyYAML package in Linux, Windows, or macOS.

Method 1: Installing PyYAML via pip

PyYAML package can be installed using pip by running the following commands on the Terminal/ Command Line:

Linux and macOS:

Python 2:

pip install pyyaml or python -m pip install pyyaml

Python 3:

pip3 install pyyaml or python3 -m pip install pyyaml

Windows:

py -m pip install pyyaml

Note (for macOS users): libyaml is a dependency of PyYAML in MacOS. In some cases, therefore, you may need to install libyaml before using the commands above to install PyYAML. To do that, execute the following command in the Terminal:

brew install libyaml

Method 2: Instaling PyYAML from the source

To do that, follow these steps:

Step 1: Download PyYAML GZ compressed tarball (.tar.gz)

Head to the PyYAML website (https://pypi.org/project/PyYAML/#files) and download the source tarball. At the time of writing this, the current stable version is v6.0. When I download the tarball, I get PyYAML-6.0.tar.gz.

Step 2a: Install the package

At this point, you can install the package using pip using any of the following commands.

Linux and macOS:

python3 -m pip install <path to the tarball> or python -m pip install <path to the tarball>

Windows:

py -m pip install <path to the tarball>

In my case, running Python 3.9 on Debian 11, I will execute the following:

python3 -m pip install ~/Downloads/PyYAML-6.0.tar.gz

If you run into any problems, proceed with the rest of the steps.

Step 2b: Unpack the source tarball

You can use the inbuilt extraction tool to extract the archive or a command line tool like tar which is available on Linux, macOS, and Windows.

tar -xf <path to th tartball> <destination>

In my case, I will execute cd ~/Downloads/ then tar -xf PyYAML-6.0.tar.gz to extract the archive into the Downloads folder.

Step 3: Install the package by building it from setup.py

Go into (cd into) the extracted folder PyYAML-** and run:

Linux/macOS/Windows:

python3 setup.py install or python setup.py install

Method 3: Installing using package manager (for Linux Users)

You can use the package manager if you want to install the PyYAML package for system-level access in Linux. Some Linux package managers include aptitude (apt) for Debian-based systems, yum or dnf for Red-Hat-based systems, pacman for Arch-based distributions, etc.

First, update the repositories by running the following

sudo apt update (for Debian-based distributions)

sudo dnf makecache -refresh or sudo yum makecache -refresh (for Red-Hat-based distros)

Secondly, install the dependencies:

sudo apt install libyaml-dev
sudo yum -y install libyaml-devel

Finally, install PyYAML by running the following commands based on your distribution.

sudo apt install python3-yaml or sudo apt install python-yaml

sudo yum install python3-yaml or sudo yum install python-yaml

Note: Packages hosted on Python Packages Index (PyPI) are generally more updated than the ones available in most Linux repositories. Therefore, Methods 1 and 2 may yield more updated packages than Method 3. For example, by the time of writing this article, the APT repository has PyYAML v5.3.1, but pip installation from PyPI yields v6.0)

Solution B: Reinstalling the PyYAML package

This section will provide solutions to cases when ImportError or ModuleNotFoundError is raised because of module path problems. The exception is raised chiefly when a conflict exists for the system-wide installation of packages.

In this solution, we uninstall and reinstall the PyYAML package.

To uninstall PyYAML and its dependencies, run the following commands (you may need to change the commands to use the package manager used by the distributions you are running):

sudo apt remove python3-yaml
sudo apt remove --auto-remove python3-yaml

Next, purge all configuration files by running the following:

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

Then reinstall the PyYAML package as discussed in Method 3.

Note: You can also implement solution B by removing PyYAML using pip and reinstalling it.

Final Shot

In the unlikely event that the above solutions don’t work, try the following: