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
- Check which Python runs your script.
Open a terminal and run:
which python(on Linux/Mac) orwhere python(on Windows).
Write down that path. For example:/usr/bin/python3.11 - 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 likepip 23.2 from /usr/lib/python3.11/site-packages/pipbut your Python is 3.9, that's your problem. - Reinstall PyYAML using the same Python.
Run:python -m pip install --upgrade --force-reinstall pyyaml
The--force-reinstallflag overwrites any broken or stale install. You should see output likeInstalling collected packages: pyyamland thenSuccessfully installed pyyaml-6.0.1. - Test the import from that same Python.
Run:python -c "import yaml; print(yaml.__version__)"
If you see a version number like6.0.1, you're good. If you still get the error, move to step 5. - Check if you're in a virtual environment.
Run:python -m siteand look forsys.path. If it includes a path like.../venv/lib/...you're in a venv. Activate it first:source venv/bin/activate(Linux/Mac) orvenv\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. Runpip list | grep yamlto 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.