{"id":1884,"library":"tree-sitter-python","title":"Python grammar for tree-sitter","description":"tree-sitter-python (version 0.25.0) provides a pre-compiled Python grammar for the tree-sitter parsing library. It enables Python applications to efficiently parse Python source code into concrete syntax trees, facilitating tasks like static analysis, code transformation, and IDE features. The library is actively maintained with regular updates following the upstream tree-sitter project.","status":"active","version":"0.25.0","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-python","tags":["parsing","syntax tree","AST","code analysis","grammar","compiler"],"install":[{"cmd":"pip install tree-sitter tree-sitter-python","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core Python bindings for the Tree-sitter parsing library.","package":"tree-sitter","optional":false},{"reason":"Requires Python 3.10 or newer. Version 0.24.0 dropped support for Python 3.9.","package":"python","optional":false}],"imports":[{"symbol":"Language","correct":"from tree_sitter import Language"},{"symbol":"Parser","correct":"from tree_sitter import Parser"},{"symbol":"tree_sitter_python","correct":"import tree_sitter_python"}],"quickstart":{"code":"from tree_sitter import Language, Parser\nimport tree_sitter_python\n\n# Initialize the Python language grammar\nPY_LANGUAGE = Language(tree_sitter_python.language())\n\n# Create a parser and configure it to use the Python language\nparser = Parser(PY_LANGUAGE)\n\n# Source code to parse\ncode = b\"\"\"\ndef greet(name):\n    print(f\"Hello, {name}!\")\n\ngreet(\"World\")\n\"\"\"\n\n# Parse the code\ntree = parser.parse(code)\n\n# Get the root node of the syntax tree\nroot_node = tree.root_node\n\n# Print the type of the root node and its first child (for demonstration)\nprint(f\"Root node type: {root_node.type}\")\nif root_node.children:\n    print(f\"First child type: {root_node.children[0].type}\")\n\n# Expected output might vary slightly but should show 'module' and 'function_definition'\n","lang":"python","description":"This quickstart demonstrates how to load the Python grammar and parse a simple Python code snippet. It initializes a `Language` object using `tree_sitter_python.language()`, creates a `Parser`, and then parses a byte string of Python code into a syntax tree. The root node's type and its first child's type are printed."},"warnings":[{"fix":"Always ensure `tree-sitter` and `tree-sitter-python` are updated to compatible versions, ideally the latest available. For example, `tree-sitter` 0.25.0 and later primarily support ABI version 15.","message":"The `tree-sitter` core library and its language grammars (like `tree-sitter-python`) use an internal ABI version. Incompatibilities can arise if the `tree-sitter` Python package and the `tree-sitter-python` grammar package are not aligned in their supported ABI versions, leading to runtime errors.","severity":"breaking","affected_versions":"tree-sitter < 0.25.0, tree-sitter-python < 0.25.0"},{"fix":"Review the official `py-tree-sitter` documentation or GitHub releases for `v0.24.0` for detailed API changes. Update code to use new signatures, e.g., using `Query(language, source)` instead of `Language.query(source)`.","message":"The `tree-sitter` core Python bindings (`py-tree-sitter` package) introduced significant breaking changes in version `0.24.0`, affecting method signatures for `Parser.parse`, `Query.captures`, `Query.matches`, and deprecated `Language.query`.","severity":"breaking","affected_versions":"py-tree-sitter >= 0.24.0"},{"fix":"Upgrade your Python environment to Python 3.10 or newer.","message":"Version 0.24.0 of `tree-sitter-python` dropped compatibility with Python 3.9.","severity":"deprecated","affected_versions":"tree-sitter-python >= 0.24.0"},{"fix":"Monitor memory usage for large inputs. If excessive memory consumption is observed, check for updates to both `tree-sitter` and the specific language grammar (`tree-sitter-python`). Consider processing code in smaller chunks if possible.","message":"When working with `tree-sitter` and its Python bindings, parsing very large or complex codebases, especially with certain grammar versions (e.g., specific `tree-sitter-bash` versions), has been reported to cause high memory usage or even memory leaks.","severity":"gotcha","affected_versions":"Potentially all versions, dependent on grammar and input size"},{"fix":"Ensure you have a C compiler (like GCC or Clang) and Node.js installed and accessible in your PATH if you intend to compile `tree-sitter` grammars from source.","message":"While `tree-sitter-python` provides a pre-compiled grammar, building custom or third-party `tree-sitter` grammars for other languages requires a C compiler and Node.js for compilation, which can be a common setup hurdle for new users.","severity":"gotcha","affected_versions":"All versions (for custom grammars)"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}