{"id":21835,"library":"pytricia","title":"PyTricia","description":"PyTricia is an efficient IP address storage and lookup module for Python, based on the Patricia trie data structure. It supports IPv4 and IPv6, longest-prefix matching, and set operations. Current version 1.3.0 is stable with no known breaking changes. Release cadence is low (last release 2025).","status":"active","version":"1.3.0","language":"python","source_language":"en","source_url":"https://github.com/jsommers/pytricia","tags":["IP","patricia-trie","routing","network"],"install":[{"cmd":"pip install pytricia","lang":"bash","label":"Default install from PyPI"},{"cmd":"pip install pytricia==1.3.0","lang":"bash","label":"Pin specific version"}],"dependencies":[],"imports":[{"note":"The module is imported directly; PyTricia is the class name.","symbol":"PyTricia","correct":"import pytricia"}],"quickstart":{"code":"import pytricia\n\n# Initialize a new trie\npt = pytricia.PyTricia()\n\n# Insert a prefix\npt[\"10.0.0.0/8\"] = \"internal\"\npt[\"10.1.0.0/16\"] = \"subnet\"\n\n# Lookup an IP\nresult = pt[\"10.1.0.5\"]\nprint(result)  # prints 'subnet'\n\n# Longest prefix match\nimport ipaddress\nnet = pt.get_key(\"10.1.0.5\")\nprint(net)  # prints '10.1.0.0/16'\n\n# Check containment\nprint(\"10.0.0.0/8\" in pt)  # True\n\n# Iterate over prefixes\nfor prefix in pt:\n    print(prefix, pt[prefix])\n\n# Delete a prefix\ndel pt[\"10.1.0.0/16\"]","lang":"python","description":"Basic usage: insert, lookup, longest-prefix match, containment check, iteration, and deletion."},"warnings":[{"fix":"Always provide a prefix length when inserting. For single IPs, use e.g., '192.168.1.1/32'.","message":"PyTricia expects prefix strings in CIDR notation (e.g., '10.0.0.0/8'). Do not pass IP addresses without prefix length; for host lookups, use the IP with /32 or use get_key() which returns the matching prefix.","severity":"gotcha","affected_versions":"all"},{"fix":"Validate input with ipaddress.ip_network(prefix, strict=False) before inserting.","message":"PyTricia does not support non-ASCII or non-CIDR inputs; all prefixes must be valid IPv4 or IPv6 CIDR strings. Passing a malformed string raises ValueError.","severity":"gotcha","affected_versions":"all"},{"fix":"Use 'prefix in pt' before accessing pt[prefix] to avoid KeyError.","message":"Values stored in the trie are not validated; they can be any Python object. However, retrieving a key that does not exist raises KeyError. Use pt.get(key, default) or check containment with 'in' first.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Ensure the IP is covered by an inserted prefix. For exact host lookup, insert with /32. Use pt.get(ip, None) to avoid exception.","cause":"Trying to look up an IP that is not contained in any inserted prefix, or using an IP without /32.","error":"KeyError: <ip_address>"},{"fix":"Check input format: e.g., '10.0.0.0/8' is valid. Use ipaddress module to parse and validate.","cause":"Passing a string that is not a valid CIDR prefix (e.g., missing slash, invalid address).","error":"ValueError: invalid prefix"},{"fix":"Ensure you have created an instance: pt = pytricia.PyTricia() then use pt[prefix] = value.","cause":"Attempting to assign a value without having imported pytricia correctly or using a read-only view.","error":"TypeError: 'PyTricia' object does not support item assignment"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}