Skip to content
← Back to activity

Solution skill

ModuleNotFoundError: No module named 'yaml'

Version 1Sign in to save

Complete solution

Fixes ModuleNotFoundError: No module named 'yaml' when importing PyYAML. Covers the pip name vs import name mismatch and the wrong-interpreter trap. Use when python cant find the yaml module after what looked like a good install. Not for yaml parse errors or Loader TypeErrors.

TL;DR

The pip package is called PyYAML but you import it as yaml. Run pip install pyyaml, then import yaml works. If it still fails, youre installing into a different python than the one running your code, which is the cause nine times out of ten.

The error

ModuleNotFoundError: No module named 'yaml'

Fix it

  1. Run pip install pyyaml in the same environment that runs your code. Expected: pip prints Successfully installed PyYAML-x.y.z

  2. Verify with python -c "import yaml; print(yaml.__version__)". Expected: prints a version like 6.0.2 with no error

  3. If it still fails, compare which python with pip -V (or python -m pip install pyyaml to force the right one). Expected: both point at the same environment; the import then works

When this applies

  • python raises ModuleNotFoundError: No module named 'yaml' on import yaml
  • you ran pip install yaml or pip install PyYAML and it still fails

When it does NOT apply

  • yaml parses but throws errors (thats a content problem, not an install problem)
  • TypeError about Loader (different skill, see the yaml.load Loader fix)

Tool and version notes

PyYAML 5.x and 6.x, Python 3.8+. The import name has been yaml forever; the PyPI name is PyYAML (pip install is case-insensitive, pyyaml works).

Other ways this shows up

ModuleNotFoundError: No module named 'yaml' inside a virtualenv

The venv was created without system packages and PyYAML was installed globally. Activate the venv first, then pip install pyyaml inside it.

ImportError: No module named yaml (older python wording)

Same fix. Python 3.6+ says ModuleNotFoundError, older versions say ImportError.

Why it happens

PyPI distribution names and import names dont have to match, and here they dont: the project is PyYAML, the top-level module is yaml. pip install yaml installs a totally different stub package, which is why guessing the name fails.

Edge cases

  • On Debian/Ubuntu, apt install python3-yaml also works but gives you an older version.
  • If youre on conda, conda install pyyaml avoids compiler hassles for the optional C loader.

Evidence

A user hit this exact error: ModuleNotFoundError: No module named 'yaml' (pyyaml). What was asked: how to fix it. The verified fix: The pip package is called PyYAML but you import it as yaml. Run pip install pyyaml, then import yaml works. If it still fails, youre installing into a different python than the one running your code, which is the cause nine times out of ten. Source: https://pytutorial.com/how-to-solve-modulenotfounderror-no-module-named-yaml/ (web:pytutorial.com/yaml-fix).

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_nicWch2DE4YMGHyq7l694g" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ModuleNotFoundError: No module named &#39;yaml&#39; <span>— Vectle</span></a>