{"id":10161,"library":"python-hostlist","title":"Python Hostlist","description":"python-hostlist is a Python module designed for parsing and expanding hostlist strings (e.g., 'host[01-03],host[05,07]'). It can expand such strings into a list of individual hostnames and also compress a list of hostnames back into a compact hostlist string. The library is actively maintained, with releases typically driven by bug fixes or minor feature additions.","status":"active","version":"2.3.0","language":"en","source_language":"en","source_url":"https://github.com/PySlurm/python-hostlist","tags":["networking","hostname","utility","parsing","slurm"],"install":[{"cmd":"pip install python-hostlist","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The installed package `python-hostlist` exposes its functionality under the `hostlist` top-level module name, not `python_hostlist`.","wrong":"import python_hostlist","symbol":"hostlist","correct":"import hostlist"}],"quickstart":{"code":"import hostlist\n\n# Expand a hostlist string\nexpanded_hosts = hostlist.expand_hostlist('host[01-03,05],server[10-12]')\nprint(f\"Expanded: {expanded_hosts}\")\n\n# Compress a list of hostnames\nhostnames = ['node01', 'node02', 'node03', 'node05', 'node07']\ncompressed_string = hostlist.compress_hostlist(hostnames)\nprint(f\"Compressed: {compressed_string}\")\n\n# Check if a string is a valid hostlist\nis_valid = hostlist.is_hostlist('test[1-5]')\nprint(f\"Is 'test[1-5]' a valid hostlist? {is_valid}\")\nis_invalid = hostlist.is_hostlist('test[1-a]')\nprint(f\"Is 'test[1-a]' a valid hostlist? {is_invalid}\")","lang":"python","description":"Demonstrates basic expansion and compression of hostlist strings, and checking for hostlist validity."},"warnings":[{"fix":"Ensure your environment uses Python 3.6 or newer. Upgrade your Python interpreter if necessary.","message":"Python 2 support was dropped in version 2.0.0. All versions >= 2.0.0 require Python 3 (specifically >=3.6).","severity":"breaking","affected_versions":">= 2.0.0"},{"fix":"Rewrite your code to use `hostlist.expand_hostlist()` and process the resulting list, or use `hostlist.compress_hostlist()` for the inverse operation. There are no direct replacements for these specific functions.","message":"The `hostlist.get_ranges()` and `hostlist.get_groups()` functions were removed in version 2.2.0.","severity":"breaking","affected_versions":">= 2.2.0"},{"fix":"Always sort your list of hostnames before passing it to `hostlist.compress_hostlist()`, e.g., `hostlist.compress_hostlist(sorted(my_hostnames))`.","message":"The `compress_hostlist` function expects a sorted list of hostnames for optimal and consistent compression. Providing an unsorted list may result in a less efficient or unexpectedly formatted hostlist string.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always validate input strings using `hostlist.is_hostlist()` or wrap calls to `expand_hostlist()` in a `try...except hostlist.HostlistFormatException` block to handle malformed input gracefully.","message":"Invalid hostlist string formats will raise a `hostlist.HostlistFormatException`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Change `import python_hostlist` to `import hostlist`.","cause":"Attempting to import the module using the package name (`python_hostlist`) instead of its internal module name (`hostlist`).","error":"ModuleNotFoundError: No module named 'python_hostlist'"},{"fix":"Review the hostlist string for syntax errors. Ensure ranges are numeric (e.g., `host[01-03]`) and separators are correct. Consider using `hostlist.is_hostlist()` to pre-validate inputs.","cause":"The input string provided to functions like `expand_hostlist` does not conform to the expected hostlist format.","error":"hostlist.HostlistFormatException: Invalid Hostlist: host[1-a]"},{"fix":"Sort the list of hostnames before passing it to the compression function: `hostlist.compress_hostlist(sorted(my_hostnames))`.","cause":"The input list of hostnames provided to `compress_hostlist` was not sorted, leading to sub-optimal compression or unusual ordering.","error":"The compressed hostlist string is not as compact or ordered as expected."}]}