Python
YAML
package installation
pip
programming tutorial

How do I install the yaml package for Python?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The usual confusion here is that the import name and the package name are different. In Python, you typically write import yaml, but the package you install is usually PyYAML. So if you try to install a package literally named yaml, you may be solving the wrong problem.

Install PyYAML

For most environments, the standard command is:

bash
python -m pip install PyYAML

After installation, use it like this:

python
1import yaml
2
3data = yaml.safe_load("name: Alice\nage: 30\n")
4print(data)

That import yaml succeeds because PyYAML exposes the module under the name yaml.

Use the Right Python Interpreter

If your machine has multiple Python versions, install with the interpreter you actually plan to run.

bash
python3 -m pip install PyYAML
python3 -c "import yaml; print(yaml.__version__)"

This avoids the classic problem where pip installs into one environment and your script runs in another.

Virtual Environments Are the Safer Default

A virtual environment keeps the dependency isolated to one project.

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install PyYAML

Then verify it:

bash
python -c "import yaml; print(yaml.__version__)"

That check is worth doing immediately, because import errors later are often just environment mismatch.

Requirements Files and Reproducibility

If this is a project dependency, add it to requirements.txt or your environment definition instead of relying only on a manual install.

text
PyYAML==6.0.2

Then install with:

bash
python -m pip install -r requirements.txt

That makes the setup repeatable across machines and CI jobs.

Common Installation Problems

One common error is ModuleNotFoundError: No module named 'yaml' even after installation. Usually that means one of these:

  • 'PyYAML was installed into a different environment'
  • the IDE is using a different interpreter
  • 'pip and python point to different Python installations'

Another issue is trying to install yaml instead of PyYAML. The import name is yaml, but the package normally installed from pip is PyYAML.

If you are using Conda, the package name can still be pyyaml:

bash
conda install pyyaml

The same import statement remains:

python
import yaml

Use Safe Loading by Default

Once the package is installed, another important practice is using safe_load unless you explicitly need advanced object construction.

python
1import yaml
2
3with open("config.yaml", "r", encoding="utf-8") as file:
4    config = yaml.safe_load(file)
5
6print(config)

This is safer for ordinary configuration parsing than using unrestricted loaders blindly.

If you are debugging an existing environment, python -m pip show PyYAML is also useful because it reveals exactly where the installed distribution lives. That often makes interpreter mismatches obvious immediately.

Once installation works, keep the dependency name consistent in documentation and onboarding notes. Many setup problems are repeated simply because one document says “install yaml” while another says “install PyYAML”.

Common Pitfalls

The biggest mistake is confusing the package name with the import name. Install PyYAML, then import yaml.

Another issue is installing into the wrong environment. If the import still fails, confirm that the same interpreter is used for installation and execution.

People also sometimes use plain pip install ... without qualifying it with python -m pip. That works often enough to feel normal, but it increases the odds of interpreter mismatch on systems with multiple Python installs.

Finally, do not ignore loader choice after installation. Parsing YAML unsafely is a separate mistake from installing the package incorrectly.

Summary

  • The package you usually install is PyYAML, but the module you import is yaml.
  • Use python -m pip install PyYAML to reduce interpreter mismatch.
  • Prefer virtual environments for project-specific installs.
  • Verify installation immediately with a one-line import test.
  • Use yaml.safe_load by default for ordinary YAML parsing.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.