{"id":1516,"library":"ipaddress","title":"ipaddress - IPv4/IPv6 Manipulation Library","description":"The `ipaddress` module provides comprehensive tools for creating, manipulating, and operating on IPv4 and IPv6 addresses and networks. It is part of the Python 3 standard library (since Python 3.3) and available as a backport on PyPI for older Python versions. It simplifies common networking tasks such as IP address and network validation, subnetting, network relationship checks, and host enumeration.","status":"active","version":"1.0.23","language":"python","source_language":"en","source_url":"https://github.com/phihag/ipaddress","tags":["networking","ip-address","ipv4","ipv6","subnetting","validation","standard-library"],"install":[{"cmd":"pip install ipaddress","lang":"bash","label":"For Python 2.6, 2.7, and 3.2 (backport)"},{"cmd":"# ipaddress is built-in since Python 3.3, no installation needed.","lang":"bash","label":"For Python 3.3+"}],"dependencies":[],"imports":[{"note":"`ipaddr` is an older, superseded library with API differences. Use `ipaddress` instead.","wrong":"import ipaddr","symbol":"ipaddress","correct":"import ipaddress"}],"quickstart":{"code":"import ipaddress\n\n# Validate and parse an IP address\nipv4_addr = ipaddress.ip_address('192.168.1.1')\nprint(f\"IPv4 Address: {ipv4_addr}, Version: {ipv4_addr.version}, Is private: {ipv4_addr.is_private}\")\n\nipv6_addr = ipaddress.ip_address('2001:db8::1')\nprint(f\"IPv6 Address: {ipv6_addr}, Version: {ipv6_addr.version}, Is global: {ipv6_addr.is_global}\")\n\n# Define and inspect an IP network\nnetwork = ipaddress.ip_network('192.168.1.0/24')\nprint(f\"Network: {network}\")\nprint(f\"Number of hosts: {network.num_addresses}\")\nprint(f\"Network address: {network.network_address}\")\nprint(f\"Broadcast address: {network.broadcast_address}\")\n\n# Check if an address is in a network\nhost_in_network = ipaddress.ip_address('192.168.1.50')\nprint(f\"{host_in_network} in {network}: {host_in_network in network}\")\n\n# Iterate over hosts in a network\n# for host in network.hosts():\n#     print(host)\n\n# Subnetting\nsupernet = ipaddress.ip_network('10.0.0.0/8')\nfor subnet in supernet.subnets(prefixlen_diff=2):\n    print(f\"Subnet: {subnet}\")","lang":"python","description":"This quickstart demonstrates how to parse IP addresses and networks, check properties like version and privacy, and perform common network operations such as membership testing and subnetting using the `ipaddress` module."},"warnings":[{"fix":"For Python 3.3+, simply `import ipaddress`. For older Python versions, `pip install ipaddress` will provide the backport.","message":"The `ipaddress` module on PyPI is a backport of the standard library module. For Python 3.3 and newer, `ipaddress` is built-in and does not require installation via pip. Ensure you are using the appropriate version for your Python environment.","severity":"gotcha","affected_versions":"<3.3 (for pip install)"},{"fix":"Migrate code to use the `ipaddress` module and its API. Avoid `ipaddr` entirely.","message":"The `ipaddr` library is an older, superseded project that has API differences from `ipaddress`. While it served a similar purpose, it has been officially superseded by `ipaddress`.","severity":"breaking","affected_versions":"All versions where `ipaddr` was used instead of `ipaddress`."},{"fix":"Ensure the host portion of network addresses is zeroed out for `Network` objects, or use `strict=False` if you understand the implications. For host addresses with a netmask, use `IPv4Interface` or `IPv6Interface`.","message":"When creating `IPv4Network` or `IPv6Network` objects, the host portion of the address must be all zeroes by default (strict mode). If you provide a host address in a network definition, a `ValueError` will be raised unless `strict=False` is passed. Even with `strict=False`, the host bits will be silently zeroed out to form a network address. For representing an address *and* its prefix (like an interface address), use `IPv4Interface` or `IPv6Interface`.","severity":"gotcha","affected_versions":"All versions of `ipaddress`."},{"fix":"Explicitly cast strings to `unicode` for textual IP addresses in Python 2, and be mindful of `bytearray` for packed representations. For new development, prioritize Python 3 where `bytes` and `str` are distinct and well-defined.","message":"In Python 2 environments using the `ipaddress` backport, there are nuances with string and byte types. Textual IP addresses should often be `unicode` strings, and packed (binary) representations might use `bytearray` instead of Python 3's distinct `bytes` type. This can lead to unexpected behavior if not handled carefully during Python 2/3 migration or interoperability.","severity":"gotcha","affected_versions":"Python 2.6, 2.7 when using the `ipaddress` backport."}],"env_vars":null,"search_vec":"'3':29 '3.3':34 'address':20,54,68 'avail':36 'backport':39 'check':61 'common':48 'comprehens':9 'creat':12 'enumer':64 'host':63 'ip':53,67 'ip-address':66 'ipaddress':1,6 'ipv4':17,69 'ipv4/ipv6':2 'ipv6':19,70 'librari':4,31,75 'manipul':3,13 'modul':7 'network':22,49,56,59,65 'older':43 'oper':15 'part':25 'provid':8 'pypi':41 'python':28,33,44 'relationship':60 'simplifi':47 'sinc':32 'standard':30,74 'standard-librari':73 'subnet':58,71 'task':50 'tool':10 'valid':57,72 'version':45","created_at":"2026-04-09T03:51:17.699052+00:00","updated_at":"2026-04-16T15:46:58.541049+00:00","problems":[{"fix":"Ensure the IP address or network string adheres to standard IPv4 (e.g., '192.168.1.1', '10.0.0.0/24') or IPv6 (e.g., '2001:db8::1', '2001:db8::/32') formatting.","cause":"This error occurs when the input string provided to `ipaddress.ip_address()` or `ipaddress.ip_network()` is not a syntactically valid IPv4 or IPv6 address/network.","error":"ValueError: '192.168.0.300' does not appear to be an IPv4 or IPv6 address"},{"fix":"Provide a network address where host bits are zero (e.g., `ipaddress.ip_network('192.168.1.0/24')`), or set `strict=False` to automatically mask out host bits (e.g., `ipaddress.ip_network('192.168.1.1/24', strict=False)`).","cause":"This error occurs when attempting to create an `IPv4Network` or `IPv6Network` object with an IP address that has non-zero host bits when the `strict` parameter (which defaults to `True`) is enabled. Network objects must represent a pure network address.","error":"ValueError: 192.168.1.1/24 has host bits set"},{"fix":"Ensure that comparisons are only made between `ipaddress` objects of the same type and IP version. Convert objects to a common comparable format (e.g., integers) if version-agnostic comparison is needed, or explicitly check versions before comparing.","cause":"This error is raised when trying to compare `ipaddress` objects of different types (e.g., `IPv4Address` with `IPv4Network`) or different IP versions (e.g., `IPv4Address` with `IPv6Address`), as these comparisons are not inherently defined.","error":"TypeError: cannot compare different types: <class 'ipaddress.IPv4Address'> and <class 'ipaddress.IPv6Address'>"},{"fix":"For Python 2.x, install the backport: `pip install ipaddress`. For Python 3.x, ensure your Python installation is complete and the module is accessible, or check for virtual environment issues.","cause":"The `ipaddress` module is part of the Python 3 standard library (since Python 3.3). This error typically occurs when trying to use `import ipaddress` in a Python 2.x environment without having installed the `ipaddress` backport from PyPI.","error":"ImportError: No module named ipaddress"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0.23","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/phihag/ipaddress","docs":null,"changelog":null,"pypi":"https://pypi.org/project/ipaddress/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}