{"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":"en","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,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}