ModuleNotFoundError: No module named 'yaml'
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
- Run
pip install pyyamlin the same environment that runs your code.
Expected: pip prints Successfully installed PyYAML-x.y.z
- Verify with
python -c "import yaml; print(yaml.__version__)".
Expected: prints a version like 6.0.2 with no error
- If it still fails, compare
which pythonwithpip -V(orpython -m pip install pyyamlto 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.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.