ModuleNotFoundError: No module named 'yaml' in Python
Codemia Knowledge Hub ·
Python cannot find the YAML library in the interpreter you are running. Install the PyYAML package into that same interpreter with python -m pip install pyyaml. If it is already installed, either a different interpreter is running your script or a local file named yaml.py is shadowing the real module.
The package name is the first trap: you write import yaml, but the package on PyPI is PyYAML, not yaml.
Install PyYAML into the right interpreter
Use python -m pip rather than bare pip, so the package lands in the interpreter that will run your script:
Where python3 is the correct binary (common on macOS and many Linux distributions):
Then verify:
pip show confirms the package is installed, and the import test confirms the interpreter can load it.
Why you install pyyaml but import yaml
Many packages use a different name on PyPI than the module you import. PyYAML installs a module named yaml. Similar cases:
| PyPI Package Name | Import Name |
| PyYAML | yaml |
| Pillow | PIL |
| scikit-learn | sklearn |
| python-dateutil | dateutil |
| beautifulsoup4 | bs4 |
pip install yaml either fails or installs a different, largely abandoned package. Always install pyyaml.
Fixing virtual environment mismatches
The most frequent cause is installing PyYAML into one environment and running the script in another: a virtual environment, a Conda environment, or a system versus user install.
Create and activate a virtual environment
See which interpreter is active
If sys.executable does not point to the environment where you installed pyyaml, that is the problem. Activate the right environment or install into the one you are using. Forgetting to activate the venv in the current shell has the same effect: the package is inside the venv, but the script runs with the system Python.
Check the IDE interpreter
VS Code, PyCharm and other IDEs keep their own interpreter setting, separate from your terminal. If the IDE uses the system Python while your terminal uses a virtual environment (or the reverse), the import fails in one place and works in the other. Point the IDE's interpreter selector at the same environment.
Fixing a local file that shadows yaml
A file named yaml.py or a directory named yaml/ in your project can be imported instead of the real package:
The current directory comes first on sys.path, so import yaml in app.py finds the local file. Rename it and delete the cached bytecode, because Python may otherwise keep loading the old __pycache__/yaml.cpython-*.pyc:
To see which file Python actually loaded:
If the path points into your project instead of site-packages, shadowing is the cause.
Using PyYAML once it imports
A minimal round trip that parses and dumps YAML:
Use safe_load and safe_dump by default. Calling yaml.load() without a Loader argument is deprecated and unsafe, because it can construct arbitrary Python objects embedded in the YAML.
Loading a configuration file from disk:
Writing it back:
sort_keys=False keeps the original key order, which is usually what you want for configuration files.
ImportError vs ModuleNotFoundError
Since Python 3.6 you may see ModuleNotFoundError instead of ImportError. It is a subclass of ImportError, so except ImportError still catches it, and the fixes are the same.
Docker and CI environments
In containers and CI, the error usually means pyyaml is missing from requirements.txt or the install step did not run.
Make sure pyyaml is listed in requirements.txt:
In CI pipelines, the install step must run before any script that imports yaml.
Related reading
- ImportError numpy.core.multiarray failed to import
- ImportError'Could not import PIL.Image. ' working with keras-ternsorflow
- Importing class from another file
- Importing module from string variable using __import__ gives different results than a normal import statement
- Importing TensorFlow fails with a SyntaxError, complaining about a parameter called async
- Importing tensorflow makes python 3.6.5 error
- Importing modules from parent folder
- Importing variables from another file?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.