{"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":"python","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,"search_vec":"'-03':20 '01':19 '05':22 '07':23 'activ':51 'addit':63 'also':36 'back':42 'bug':58 'compact':45 'compress':37 'design':10 'driven':56 'e.g':17 'expand':14,26 'featur':62 'fix':59 'host':18,21 'hostlist':2,5,15,46 'hostnam':34,41,65 'individu':33 'librari':49 'list':31,39 'maintain':52 'minor':61 'modul':9 'network':64 'pars':12,67 'python':1,4,8 'python-hostlist':3 'releas':54 'slurm':68 'string':16,28,47 'typic':55 'util':66","created_at":"2026-04-17T01:22:42.444110+00:00","updated_at":"2026-04-17T01:22:42.444110+00:00","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."}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.3.0","cli_name":"hostlist","cli_version":"Version 2.3.0 (library version 2.3.0)","type":"library","homepage":"http://www.nsc.liu.se/~kent/python-hostlist/","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/python-hostlist/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}