{"id":6660,"library":"graphlib-backport","title":"graphlib-backport","description":"Backport of the Python 3.9 `graphlib` module for Python 3.6+. It provides the `TopologicalSorter` class for performing topological sorting on directed acyclic graphs (DAGs). The library is currently at version 1.1.0 and is actively maintained, with releases tied to updates on its GitHub repository.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/mariushelf/graphlib_backport","tags":["graph","topological-sort","backport","python3.9","utility"],"install":[{"cmd":"pip install graphlib-backport","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The backport uses 'graphlib_backport' as its top-level package name. 'graphlib' would refer to the standard library module, which only exists in Python 3.9+.","wrong":"from graphlib import TopologicalSorter","symbol":"TopologicalSorter","correct":"from graphlib_backport import TopologicalSorter"}],"quickstart":{"code":"from graphlib_backport import TopologicalSorter\n\n# Example graph: keys are nodes, values are sets of predecessors\ngraph = {\"D\": {\"B\", \"C\"}, \"C\": {\"A\"}, \"B\": {\"A\"}, \"A\": set()}\n\nts = TopologicalSorter(graph)\n\n# Get the nodes in topological order using static_order (convenience method)\norder = list(ts.static_order())\nprint(f\"Topological order (static): {order}\")\n\n# Example with dynamic processing (simulating parallel work)\nprint(\"\\nProcessing nodes dynamically:\")\nts_dynamic = TopologicalSorter(graph)\nts_dynamic.prepare() # Must be called before is_active, get_ready, or done\n\nprocessed_nodes = []\nwhile ts_dynamic.is_active():\n    ready_nodes = ts_dynamic.get_ready()\n    if ready_nodes:\n        print(f\"  Ready to process: {ready_nodes}\")\n        # Simulate processing for each ready node\n        for node in ready_nodes:\n            processed_nodes.append(node)\n        ts_dynamic.done(*ready_nodes) # Mark nodes as done to unblock successors\n    else:\n        # This branch should not be reached in a valid DAG if is_active is true\n        # unless there's an unexpected state (e.g., cycle detected after prepare, or bug).\n        print(\"  No nodes ready, but sorter is active. Potential issue or cycle?\")\n        break\n\nprint(f\"Dynamic processing complete. Order: {processed_nodes}\")","lang":"python","description":"This quickstart demonstrates the core functionality of `TopologicalSorter` to process a graph. It shows both obtaining a full static order and processing nodes dynamically, which is useful for parallel execution patterns. Nodes are represented as hashable objects (e.g., strings), and dependencies are defined by providing a set of predecessors for each node."},"warnings":[{"fix":"For Python 3.9+, prefer `from graphlib import TopologicalSorter`. If you must use the backport on 3.9+, ensure your imports are explicitly `from graphlib_backport import TopologicalSorter`. Utilize environment markers or poetry's `python` dependency specifier to conditionally install the backport only on older Python versions.","message":"When installed on Python versions 3.9 or higher, the native `graphlib` module (which includes `TopologicalSorter`) will take precedence if you try to import directly as `import graphlib`. It is generally recommended to limit installation of `graphlib-backport` to Python versions `<3.9` and use the standard library's `graphlib` for Python `>=3.9`.","severity":"gotcha","affected_versions":"Python 3.9+"},{"fix":"Migrate your project to Python 3.8 or newer. For Python 3.9+, use the standard library's `graphlib` module.","message":"Explicit support for Python 3.6 and 3.7 is considered 'somewhat experimental' and is slated to be dropped, as these Python versions have reached their official end-of-life status.","severity":"deprecated","affected_versions":"Python 3.6, 3.7"},{"fix":"Use `pip install graphlib-backport` for installation and `from graphlib_backport import TopologicalSorter` (or `import graphlib_backport`) for importing in your Python code.","message":"The PyPI package name for installation is `graphlib-backport` (with a hyphen), but the Python import name within your code is `graphlib_backport` (with an underscore). Attempting `import graphlib-backport` will result in a `ModuleNotFoundError`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}