Registry / http-networking / ipaddress

ipaddress

JSON →
library1.0.23pypypi✓ verified 31d ago

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.

# Nothing to install - ipaddress is in the standard library. import ipaddress
INSTALL
IMPORT
SIG · IPADDRESS
I
ipaddress
http-networkingpythonv1.0.23
Install
1.6s avg
Import
—
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.0.23 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ipaddress
✓ import ipaddress
✗ import ipaddr
`ipaddr` is an older, superseded library with API differences. Use `ipaddress` instead.

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.

import ipaddress # Validate and parse an IP address ipv4_addr = ipaddress.ip_address('192.168.1.1') print(f"IPv4 Address: {ipv4_addr}, Version: {ipv4_addr.version}, Is private: {ipv4_addr.is_private}") ipv6_addr = ipaddress.ip_address('2001:db8::1') print(f"IPv6 Address: {ipv6_addr}, Version: {ipv6_addr.version}, Is global: {ipv6_addr.is_global}") # Define and inspect an IP network network = ipaddress.ip_network('192.168.1.0/24') print(f"Network: {network}") print(f"Number of hosts: {network.num_addresses}") print(f"Network address: {network.network_address}") print(f"Broadcast address: {network.broadcast_address}") # Check if an address is in a network host_in_network = ipaddress.ip_address('192.168.1.50') print(f"{host_in_network} in {network}: {host_in_network in network}") # Iterate over hosts in a network # for host in network.hosts(): # print(host) # Subnetting supernet = ipaddress.ip_network('10.0.0.0/8') for subnet in supernet.subnets(prefixlen_diff=2): print(f"Subnet: {subnet}")
Debug
Known issues
breaking`ipaddress` is part of the standard library in every Python we test (3.9-3.13). The PyPI package of the same name is a backport for Python versions that are all end-of-life. Installing it on a supported Python shadows the standard-library module and breaks imports in ways that are hard to diagnose, because the failure appears in unrelated code.
fix
Do not install it. `import ipaddress` works out of the box. If a requirements file pins it, delete the line.
affects: Python 3.9+
gotchaThe `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.
fix
For Python 3.3+, simply `import ipaddress`. For older Python versions, `pip install ipaddress` will provide the backport.
affects: <3.3 (for pip install)
breakingThe `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`.
fix
Migrate code to use the `ipaddress` module and its API. Avoid `ipaddr` entirely.
affects: All versions where `ipaddr` was used instead of `ipaddress`.
gotchaWhen 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`.
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`.
affects: All versions of `ipaddress`.
gotchaIn 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.
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.
affects: Python 2.6, 2.7 when using the `ipaddress` backport.
Errors
Common errors & fixes
ValueError: '192.168.0.300' does not appear to be an IPv4 or IPv6 address
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.
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.
ValueError: 192.168.1.1/24 has host bits set
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.
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)`).
TypeError: cannot compare different types: <class 'ipaddress.IPv4Address'> and <class 'ipaddress.IPv6Address'>
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.
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.
ImportError: No module named ipaddress
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.
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.
Upgrade
Version history
1.0.23latest on PyPI · released Oct 18, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Amazon
1
Anthropic
1
Resources
ipaddress — pip install ipaddress · libregistry