ImportError: cannot import name 'CLoader' from 'yaml'
Fixes ImportError: cannot import name 'CLoader' from 'yaml' when the C extension was never built. Shows the SafeLoader fallback and how to get a libyaml build. Use when CLoader/CParser/CDumper imports fail but plain yaml imports fine. Not for No module named 'yaml'.
TL;DR
CLoader only exists when PyYAML compiled against libyaml. Plenty of installs skip it (pure-python wheel, missing compiler, musl/alpine). If you just need speed-safe parsing, use yaml.SafeLoader instead, it is always there. If you truly need the C loader, reinstall so it builds: pip install --no-binary :all: pyyaml with a compiler and libyaml headers present.
The error
ImportError: cannot import name 'CLoader' from 'yaml'Fix it
Try
from yaml import SafeLoader/CSafeLoaderand confirm which exists:python -c "from yaml import SafeLoader; print('ok')". Expected: prints okFor the C loader, install build deps (gcc, libyaml-dev / libyaml-devel), then
pip install --force-reinstall --no-binary :all: pyyaml. Expected: pip build log shows building against libyaml; from yaml import CLoader then worksOr sidestep it: most code that wants CLoader runs fine on SafeLoader, just a bit slower. Expected: your YAML still parses, no import error
When this applies
- ImportError: cannot import name 'CLoader' (or CParser / CDumper) from 'yaml'
- plain import yaml works but the C names dont
When it does NOT apply
- ModuleNotFoundError: No module named 'yaml' (package not installed at all)
- you need the C loader for correctness rather than speed (you almost certainly dont)
Tool and version notes
PyYAML 5.x/6.x. Manylinux wheels bundle libyaml so CLoader usually exists there; alpine/musl, --no-binary installs, and some conda builds are the common pure-python cases.
Other ways this shows up
ImportError: cannot import name 'CSafeLoader' from 'yaml'
Same fix. All four C names (CLoader, CParser, CDumper, CSafeLoader) come from the same optional extension.
Why it happens
The C extension is a build-time option, not a separate package. If the wheel you got was pure python, the names simply arent defined, and no pip flag can conjure them without a rebuild.
Edge cases
- On alpine, you need yaml-dev plus gcc and musl-dev before the reinstall.
- Some distros split libyaml headers into a -dev package; the runtime lib alone isnt enough to build against.