ModuleNotFoundError: No module named 'yaml'

Python can't find 'yaml' even after PyYAML is installed

You installed PyYAML but Python still says 'no module named yaml'. The fix is almost always a Python environment mismatch or a broken pip install.

You wrote pip install pyyaml, it said success, but when you run import yaml you get ModuleNotFoundError: No module named 'yaml'. I've seen this pop up in two places: when you're writing a script that reads a config file, or when you're running a CI pipeline that loads a YAML file. The error's a liar — it's not that PyYAML didn't install. It means Python can't find what you installed.

Root cause: Python/pip version mismatch

You installed PyYAML with one Python version but your script runs with another. This happens when you have Python 3.9 and Python 3.11 both on the same machine, or when you use a virtual environment incorrectly. I've seen people run pip3 but call python which points to Python 2. Or they use sudo pip which installs to the system Python, but their user Python doesn't see it.

The fix: install PyYAML to the same Python that runs your script

  1. Check which Python runs your script.
    Open a terminal and run:
    which python (on Linux/Mac) or where python (on Windows).
    Write down that path. For example: /usr/bin/python3.11
  2. Check which pip matches that Python.
    Run: python -m pip --version — this shows the pip that your Python sees. The path in the output should match the Python path from step 1. If it says something like pip 23.2 from /usr/lib/python3.11/site-packages/pip but your Python is 3.9, that's your problem.
  3. Reinstall PyYAML using the same Python.
    Run: python -m pip install --upgrade --force-reinstall pyyaml
    The --force-reinstall flag overwrites any broken or stale install. You should see output like Installing collected packages: pyyaml and then Successfully installed pyyaml-6.0.1.
  4. Test the import from that same Python.
    Run: python -c "import yaml; print(yaml.__version__)"
    If you see a version number like 6.0.1, you're good. If you still get the error, move to step 5.
  5. Check if you're in a virtual environment.
    Run: python -m site and look for sys.path. If it includes a path like .../venv/lib/... you're in a venv. Activate it first: source venv/bin/activate (Linux/Mac) or venv\Scripts\activate (Windows), then repeat steps 3 and 4 inside the venv.

What to check if it still fails

  • You might be using an IDE with its own Python. VSCode, PyCharm, or VS sometimes pick a different interpreter. In VSCode, check the bottom-left status bar — it shows the selected Python. Change it to match the one where you installed PyYAML.
  • Permissions could block the install. If you used sudo pip install pyyaml, the package went to a system location. Run pip list | grep yaml to see if it's listed. If not, install without sudo: pip install --user pyyaml.
  • Your Python might be 32-bit when you need 64-bit. Unlikely but I've seen it on Windows. Run python -c "import struct; print(struct.calcsize('P') * 8)" — if it says 32, you might need a 64-bit Python if your system is 64-bit.
  • Corrupted pip cache. Run python -m pip cache purge, then reinstall PyYAML.

This exact fix has worked for every help desk ticket I've handled on this error. The real culprit is almost never PyYAML itself — it's always which Python sees it. Match your pip to your Python, and you're done.

Related Errors in Programming & Dev Tools
0X000002B8 That Debugger Control Break (0x000002B8) Is Killing Your Workflow 0XC0000373 Stack Switch Error 0xC0000373: Memory Fixes java.lang.OutOfMemoryError: Java heap space Fix Java OutOfMemoryError: Java heap space 0XC00002B5 Fixing STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5) in Windows apps

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.