{"id":27097,"library":"kdtree","title":"kdtree","description":"A Python implementation of a kd-tree for efficient spatial searching. Current version 0.16. Intermittent releases, last updated 2017.","status":"maintenance","version":"0.16","language":"python","source_language":"en","source_url":"https://github.com/stefankoegl/kdtree","tags":["kd-tree","spatial","search","nearest-neighbor"],"install":[{"cmd":"pip install kdtree","lang":"bash","label":"Install kdtree from PyPI"}],"dependencies":[],"imports":[{"note":"Avoid using 'import kdtree' because then you need to qualify everything as kdtree.KDTree, which is not typical usage.","wrong":"import kdtree","symbol":"KDTree","correct":"from kdtree import KDTree"},{"note":"There is no 'create' function; you instantiate KDTree directly.","wrong":"","symbol":"create","correct":"from kdtree import KDTree"}],"quickstart":{"code":"from kdtree import KDTree\n\n# Create a KDTree from a list of points (each point is a tuple)\npoints = [(2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2)]\ntree = KDTree(points)\n\n# Search for the nearest neighbor to a query point\nquery = (9, 2)\nnearest = tree.search_nearest(query)\nprint(f'Nearest neighbor to {query}: {nearest}')","lang":"python","description":"Create a kd-tree from points and perform a nearest neighbor search."},"warnings":[{"fix":"Upgrade to >=0.12 and use 'search_nearest' for consistent single-result behavior.","message":"The 'search_nn' method returns a single result in older versions; newer versions return a list.","severity":"breaking","affected_versions":"<0.12"},{"fix":"Pass all points at construction time: KDTree(points).","message":"The 'add' method does not exist in v0.16; use the constructor to build the tree.","severity":"deprecated","affected_versions":"0.16"},{"fix":"Ensure all point coordinates are numeric (int or float).","message":"Points must be tuples/lists of numbers; mixing types (e.g., int and float) is fine, but non-numeric types cause errors.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run 'pip install kdtree'","cause":"The library is not installed.","error":"ModuleNotFoundError: No module named 'kdtree'"},{"fix":"Use 'search_nearest' instead of 'search_nn'.","cause":"Method renamed in newer versions.","error":"AttributeError: 'KDTree' object has no attribute 'search_nn'"},{"fix":"Create tree with points: tree = KDTree([(1,2), (3,4)])","cause":"KDTree requires a list of points as argument.","error":"TypeError: __init__() takes at least 2 arguments (1 given)"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}