Skip to main content

Last updated: April 28, 2026

Fix: ModuleNotFoundError: No module named 'yaml'

The fastest fix for the Python ImportError, plus how to handle the trickier cases involving virtual environments, Conda, macOS system Python, and Docker.

Written by Mohan Raj Kolavi.

Quick fix: install PyYAML

Run pip install pyyaml. The PyPI package is called pyyaml even though the Python import name is yaml. If pip says "already satisfied" but you still see the error, run python -m pip install pyyaml to make sure the install goes into the same interpreter that runs your script.

The error you are seeing

traceback
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import yaml
ModuleNotFoundError: No module named 'yaml'

This error means Python successfully started, found your script, hit import yaml, and could not find a package named yaml on its module search path. The fix is to install pyyaml, which provides the yamlmodule.

Fix 1: pip install pyyaml

The most common case. Note that the PyPI package name and the import name differ:

install.sh
# The package is called pyyaml, not yaml
pip install pyyaml

# Or, with python3 explicitly
python3 -m pip install pyyaml

pip install yaml will not work - that name does not point to PyYAML on PyPI.

Fix 2: virtual environments

If you use a virtual environment, activate it before installing PyYAML. Otherwise the package goes into the system Python and your venv keeps failing:

venv-install.sh
# Activate the virtual environment first
source .venv/bin/activate           # macOS / Linux
.\.venv\Scripts\activate           # Windows PowerShell

# Then install
pip install pyyaml

# Verify
python -c "import yaml; print(yaml.__version__)"

The (myenv) prefix in your shell prompt confirms the venv is active. If it is missing, run the activate command again.

Fix 3: Conda environments

Conda manages its own package set. If you mix pip and conda in the same env, you can still hit this error because conda may not recognize pip-installed packages until refreshed:

conda-install.sh
# Conda environments
conda activate myenv
conda install pyyaml

# Or with conda-forge
conda install -c conda-forge pyyaml

Fix 4: which python is pip targeting?

The trickiest case is when pip says the package is installed but the import still fails. That happens when pip is attached to one Python install while your script runs under a different one. Diagnose like this:

diagnose.sh
# Confirm which python pip is installing into
python -c "import sys; print(sys.executable)"
which python
which pip

# If they point to different installs, force the same one
python -m pip install pyyaml

On macOS this is especially common because you may have system Python at /usr/bin/python3, Homebrew Python at /opt/homebrew/bin/python3, and a pyenv-managed Python on top. python -m pip install always targets the interpreter you ran python with, so prefer it over a bare pip install.

Fix 5: Docker images

If your container hits the error at runtime, the image was built without PyYAML. Add it to the Dockerfile or requirements.txt and rebuild:

Dockerfile
# Add to your Dockerfile
RUN pip install --no-cache-dir pyyaml

# Or in requirements.txt
echo "pyyaml>=6.0" >> requirements.txt
docker build -t myapp .

Should I use ruamel.yaml instead?

ruamel.yaml is a YAML 1.2 compliant alternative to PyYAML. It preserves comments and round-trips cleanly, which PyYAML does not. But its import name is ruamel.yaml, not yaml, so installing it does not fix the original error. If your existing code uses import yaml, install pyyaml. If you are starting fresh and need comment preservation, consider ruamel.yaml instead.

Verify the fix

After installing, confirm everything is wired up correctly:

  1. python -c "import yaml; print(yaml.__version__)" prints a version number (e.g. 6.0.1).
  2. Re-run your script. The ModuleNotFoundError should be gone.
  3. If the error returns later, check that the same venv or Conda env is still active.

Frequently Asked Questions

Why does Python say 'No module named yaml'?

Python's PyYAML package is not installed in the interpreter you are running. The package is called 'pyyaml' on PyPI but the import name is 'yaml', which causes confusion. Install it with: pip install pyyaml.

Is the pip package called yaml or pyyaml?

The pip package is called pyyaml. Running 'pip install yaml' may fail or install a different unrelated package. Always use 'pip install pyyaml'. After installation you import it as 'import yaml'.

What if pip install pyyaml says 'already satisfied' but I still get the error?

You almost certainly have multiple Python installations. The pip command installed PyYAML into a different interpreter than the one running your script. Run 'python -m pip install pyyaml' to install into the same interpreter you use to run code.

How do I install PyYAML in a virtual environment?

Activate the venv first (source .venv/bin/activate on macOS / Linux, .venv\Scripts\activate on Windows). Then run 'pip install pyyaml'. Verify with 'python -c "import yaml"'. If activation is missing, the install goes into the system Python, not the venv.

How do I install PyYAML in Conda?

Activate the Conda environment with 'conda activate myenv', then 'conda install pyyaml'. Conda will use its own packages instead of pip-installed ones. For the latest version, use 'conda install -c conda-forge pyyaml'.

Why does this error appear on macOS even after installing?

macOS ships with a system Python that is read-only protected. pip may install into a user directory that is not on the script's path, or a Homebrew-installed Python may take precedence. Always use 'python3 -m pip install --user pyyaml' or a virtual environment to avoid this.

How do I fix this in a Docker container?

Add 'RUN pip install --no-cache-dir pyyaml' to your Dockerfile, or include 'pyyaml' in requirements.txt and rebuild the image. The error appears at runtime if the package was not installed during the image build.

Does PyYAML have a C extension that needs separate install?

PyYAML installs a pure-Python implementation by default. Optional C bindings (libyaml-dev) speed up parsing but are not required for the import to work. The 'No module named yaml' error means the Python wrapper itself is missing.

Will ruamel.yaml fix this error?

ruamel.yaml is a different package and uses 'import ruamel.yaml', not 'import yaml'. Installing it does not provide the 'yaml' module. To fix the original ImportError, install pyyaml with 'pip install pyyaml'.

How do I check if PyYAML is installed correctly?

Run 'python -c "import yaml; print(yaml.__version__)"'. If it prints a version number (e.g. 6.0.1), PyYAML is correctly installed and importable. If it raises ModuleNotFoundError, the install did not target the active interpreter.

Fix: ModuleNotFoundError: No module named 'yaml' (Python 2026) | Kolavi Studio