{"id":9258,"library":"python-hosts","title":"Python Hosts File Manager","description":"A Python library for managing hosts files, allowing you to add, remove, import, and query entries. Version 1.1.2 was released on June 24, 2025, indicating an active maintenance status. It maintains compatibility with Python 2.7 and Python 3.5+, including PyPy variants.","status":"active","version":"1.1.2","language":"en","source_language":"en","source_url":"https://github.com/jonhadfield/python-hosts","tags":["hosts","system","networking","admin","dns"],"install":[{"cmd":"pip install python-hosts","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The `Hosts` class is nested within the `python_hosts` package.","wrong":"import Hosts","symbol":"Hosts","correct":"from python_hosts import Hosts"},{"note":"`HostsEntry` is directly available from the top-level `python_hosts` package, not a submodule.","wrong":"from python_hosts.hosts import HostsEntry","symbol":"HostsEntry","correct":"from python_hosts import HostsEntry"}],"quickstart":{"code":"from python_hosts import Hosts, HostsEntry\nimport os\n\n# Create a Hosts instance, using a test file to avoid modifying the system hosts file directly\nhosts_file_path = os.path.join(os.getcwd(), 'hosts_test')\nmy_hosts = Hosts(path=hosts_file_path)\n\n# Add a new entry\nnew_entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=['localhost.dev', 'myapp.local'])\nmy_hosts.add([new_entry])\n\n# Add another entry, demonstrating merging names\nadditional_entry = HostsEntry(entry_type='ipv4', address='192.168.1.100', names=['backend.api'])\nmy_hosts.add([additional_entry])\n\n# Merge names to an existing entry\nmerge_entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=['dev.server'])\nmy_hosts.add([merge_entry], merge_names=True)\n\n# Write changes back to the file\nmy_hosts.write()\n\nprint(f\"Hosts file updated at: {hosts_file_path}\")\n\n# Verify content (optional)\nwith open(hosts_file_path, 'r') as f:\n    print(\"\\n--- Current hosts_test content ---\")\n    print(f.read())\n\n# Clean up the test file\nos.remove(hosts_file_path)","lang":"python","description":"This quickstart demonstrates how to initialize a `Hosts` object, add new entries using `HostsEntry`, and persist changes to a specified hosts file. For safety, it uses a temporary test file instead of the system's default hosts file. It also shows how to merge additional hostnames to an existing IP address entry."},"warnings":[{"fix":"Always call `hosts_instance.write()` after making any modifications (add, remove, etc.) to ensure changes are saved to the hosts file.","message":"Changes made via `python-hosts` are not persisted until the `.write()` method is explicitly called on the `Hosts` object. Failing to call `.write()` will result in all modifications being lost.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For testing or non-privileged operations, specify a `path` to a temporary or user-owned file (e.g., `Hosts(path='my_test_hosts')`). For production use cases modifying the system hosts file, ensure your script runs with appropriate elevated permissions.","message":"When initializing `Hosts()`, if `path` is not provided, the library attempts to determine the default hosts file path for the current operating system (e.g., `/etc/hosts` on Linux, `C:\\Windows\\System32\\drivers\\etc\\hosts` on Windows). Directly modifying this file often requires elevated permissions (root/administrator), which can lead to `PermissionError` or `FileNotFoundError` if the script lacks the necessary privileges.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Develop new projects targeting Python 3.5+ or newer. If maintaining legacy Python 2.7 code, be aware of the end-of-life status for Python 2 itself.","message":"While `python-hosts` supports Python 2.7, new development should prioritize Python 3.x to leverage modern language features and benefit from continued community support and security updates. Relying on Python 2.7 for new projects is generally discouraged.","severity":"gotcha","affected_versions":"Compatibility across 2.7 and 3.5+"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run the Python script with elevated permissions (e.g., `sudo python your_script.py` on Linux/macOS, or 'Run as administrator' on Windows). Alternatively, specify a `path` to a user-writable file for development or testing: `my_hosts = Hosts(path='my_custom_hosts_file.txt')`.","cause":"The script attempted to write to the system hosts file without sufficient administrative/root privileges.","error":"PermissionError: [Errno 13] Permission denied: '/etc/hosts' (or Windows equivalent)"},{"fix":"Change the import statement to `from python_hosts import Hosts, HostsEntry`.","cause":"Incorrect import statement. The main classes (`Hosts`, `HostsEntry`) are part of the `python_hosts` package, not a module named `hosts`.","error":"ImportError: cannot import name 'Hosts' from 'hosts' (or similar 'ModuleNotFoundError: No module named 'hosts')"},{"fix":"Ensure `my_hosts.write()` is called after all desired additions, removals, or other modifications to the `Hosts` object.","cause":"The `.write()` method was not called on the `Hosts` object after modifications, so changes were not saved to disk.","error":"Changes not reflected in hosts file after running script"}]}