How to Read a YAML File in Python with yaml.safe_load
YAML is a common format for settings files, because people can read and edit it easily. In Python, the PyYAML package reads one with yaml.safe_load, which turns the file into dictionaries, lists, text and numbers. This guide reads a small settings file with PyYAML 6.0.4 on Python 3.15.2.
Syntax
import yaml
with open("<file>.yaml", encoding="utf-8") as file:
<data> = yaml.safe_load(file)
| Part | What it means |
|---|---|
import yaml | Loads PyYAML. The package is called pyyaml, but the module you import is yaml |
open("<file>.yaml", encoding="utf-8") | Opens the file as text |
yaml.safe_load(file) | Reads the file and returns plain Python values: a dictionary for key: value lines |
The PyYAML documentation says: "safe_load(stream) parses the given stream and returns a Python object constructed from for the first document in the stream." It adds that it "recognizes only standard YAML tags and cannot construct an arbitrary Python object."
The Example
A file called shop.yaml holds three settings for a bike shop: its name, its opening hours and whether it delivers. A script reads the file and uses one of the numbers.
Prerequisites
- PyYAML in your project, for example with
uv add pyyaml. This guide uses PyYAML 6.0.4 on Python 3.15.2.
Step 1: Create the File and the Script
Save this as shop.yaml:
name: Bike shop
open_hours: 8
delivery: true
Then the script, in the same folder:
import yaml
with open("shop.yaml", encoding="utf-8") as file:
settings = yaml.safe_load(file)
print(settings)
print(settings["open_hours"] + 1)

Step 2: Run It
uv run python read_yaml.py
{'name': 'Bike shop', 'open_hours': 8, 'delivery': True}
9

The file became a dictionary. 8 came back as a number, so open_hours + 1 gave 9, and true became Python's True. The command works unchanged in PowerShell.
Why safe_load and Not load
yaml.load can build any Python object a file asks for, so a file from someone else could make it run code. The PyYAML documentation warns: "It is not safe to call yaml.load with any data received from an untrusted source!" safe_load refuses such tags. Given the text pick: !!python/object/apply:os.getcwd [], it stopped with:
yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:python/object/apply:os.getcwd'
in "<unicode string>", line 1, column 7:
pick: !!python/object/apply:os.getcwd []
^
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling yaml.load(file) | TypeError: load() missing 1 required positional argument: 'Loader' | Use yaml.safe_load(file) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
yaml.safe_load returns None | The file is empty: an empty .yaml file printed None | Check that the file has content |
could not determine a constructor for the tag | The file uses a Python-specific tag such as !!python/object | Keep files to plain YAML values; only load such tags from files you wrote yourself |
FAQ
What does safe_load return for a list in the file?
A Python list. YAML mappings become dictionaries, sequences become lists, and plain values become strings, numbers, True, False or None.
Does safe_load read more than one document?
No, only the first. yaml.safe_load_all reads every document in a file that separates them with ---.
Conclusion
yaml.safe_load turned shop.yaml into a dictionary with a number and a True in it. It reads plain YAML only, which is what a settings file needs, and it refused a tag that would have run code.
Additional Resources
- PyYAML documentation —
safe_load,safe_load_alland the loading warning. - PyYAML on PyPI — the package and its versions.
