{"id":9201,"library":"py-radix","title":"py-radix","description":"py-radix is a Python library that implements the radix tree (also known as a Patricia tree) data structure, primarily for efficient storage and retrieval of IPv4 and IPv6 network prefixes. This structure is commonly used for routing table lookups due to its ability to efficiently store prefixes of varying lengths and perform fast lookups of containing networks. The library is actively maintained, with version 1.1.0 released in late 2025, and consistently supports recent Python versions.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/mjschultz/py-radix","tags":["radix-tree","networking","ip-routing","ipv4","ipv6","trie","data-structure"],"install":[{"cmd":"pip install py-radix","lang":"bash","label":"Latest stable version"}],"dependencies":[],"imports":[{"note":"The top-level package name for import is 'radix', not 'py_radix' or 'py-radix'.","symbol":"Radix","correct":"import radix"}],"quickstart":{"code":"import radix\n\n# Create a new tree\nrtree = radix.Radix()\n\n# Add nodes. Adding a node returns a RadixNode object.\n# You can store arbitrary data in its 'data' dict.\nrnode = rtree.add(\"10.0.0.0/8\")\nrnode.data[\"description\"] = \"Internal Network A\"\n\n# You can also specify networks using separate network and masklen, or packed binary addresses.\nrnode = rtree.add(network=\"172.16.0.0\", masklen=24)\nrnode.data[\"location\"] = \"Branch Office\"\n\n# IPv6 prefixes are fully supported\nrtree.add(\"2001:DB8::/32\")\n\n# Exact search returns a RadixNode if the prefix exists\nfound_node = rtree.search_exact(\"10.0.0.0/8\")\nif found_node:\n    print(f\"Found exact match: {found_node.prefix}, Data: {found_node.data}\")\n\n# Best-match search returns the longest matching prefix\nbest_match = rtree.search_best(\"10.0.0.123\")\nif best_match:\n    print(f\"Best match for 10.0.0.123: {best_match.prefix}, Data: {best_match.data}\")\n\n# Iterate over all nodes in the tree\nprint(\"\\nAll prefixes in the tree:\")\nfor node in rtree:\n    print(node.prefix)","lang":"python","description":"This quickstart demonstrates how to create a Radix tree, add IPv4 and IPv6 network prefixes with associated data, and perform exact and best-match lookups. It also shows how to iterate through all stored nodes."},"warnings":[{"fix":"Ensure C build tools are properly installed for your system. If encountering persistent build errors, particularly on newer Python versions, try setting the environment variable `RADIX_NO_EXT=1` before `pip install py-radix` to force installation of the pure Python version (though this may have performance implications). Check the GitHub issues for specific patches or workarounds for your environment.","message":"Installation issues observed with Python 3.13+ and GCC 14+ due to stricter type checking during the C extension compilation. While recent versions (1.1.0) include support for Python 3.14's free-threaded build, specific compiler/Python combinations may still face compilation errors.","severity":"breaking","affected_versions":"0.10.0 and potentially earlier versions with Python 3.13+/GCC 14+"},{"fix":"Avoid adding or deleting nodes within a `for rnode in rtree:` loop. If you need to modify the tree based on its contents, collect the necessary modifications and apply them after the iteration completes. Modifying a node's `data` dictionary during iteration is safe.","message":"Modifying the Radix tree (adding or deleting nodes) while iterating over it can lead to a `RuntimeWarning` and abort the iteration. The internal state of the tree can become inconsistent if structural changes are made during traversal.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To bypass C extension compilation and install the pure Python version, set the environment variable `RADIX_NO_EXT=1` before running `pip install py-radix`. Example: `RADIX_NO_EXT=1 pip install py-radix`.","message":"By default, `py-radix` attempts to build a C extension for performance. If C compilation fails (e.g., missing build tools, incompatible compiler), the installation might error out.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `py-radix` is installed in your active Python environment by running `pip install py-radix`. If using virtual environments, ensure your environment is activated.","cause":"The `py-radix` package was not successfully installed, or the Python environment where it was installed is not active. The Python import statement uses `import radix` while the PyPI package name is `py-radix`.","error":"ModuleNotFoundError: No module named 'radix'"},{"fix":"Install the necessary C build tools for your operating system. For Linux, this often involves `sudo apt-get install build-essential` (Debian/Ubuntu) or equivalent. For Windows, install 'Desktop development with C++' from Visual Studio Installer. As a workaround, you can install the pure Python version by setting `RADIX_NO_EXT=1` before installation: `RADIX_NO_EXT=1 pip install py-radix`.","cause":"This error typically indicates that the C extension part of `py-radix` failed to compile. This can be due to missing C development tools (e.g., GCC on Linux, Visual C++ build tools on Windows) or incompatibilities with newer Python versions or compilers.","error":"ERROR: Command errored out with exit status 1: ... Building wheel for py-radix (pyproject.toml) did not run successfully."}]}