ifaddr is a small, cross-platform Python library designed for enumerating network interfaces and their associated IP addresses. It supports Linux, OS X, Windows, and is expected to work on other BSD derivatives and Solaris/Illumos. Released under the MIT License, the current version 0.2.0 (June 2022) works with Python 3.9+ and requires no compilation, simplifying installation.
pip install ifaddrVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to retrieve all network adapters and iterate through their configured IP addresses. It also shows how to include unconfigured adapters if needed.
Upgrade to Python 3.7+ (preferably 3.9+ as per the latest README) if using ifaddr 0.2.0 or newer.
Use `ifaddr.get_adapters(include_unconfigured=True)` to retrieve all interfaces, regardless of IP configuration.
Be aware that `ip.ip` for IPv6 addresses is a tuple. Access the string representation as `ip.ip[0]` if only the address string is needed.
Prefer `adapter.nice_name` for displaying adapter names to users, especially on Windows.
Consider `netifaces` if more in-depth interface details (e.g., multicast addresses, status flags) are required, acknowledging its compilation dependency. For a subset of `netifaces` functionality, explore `ifaddr.netifaces` module.
Install the 'ifaddr' package using pip: `pip install ifaddr`
Ensure `ifaddr.get_adapters()` is spelled correctly. If a local file named `ifaddr.py` exists in your project directory, rename it to avoid conflicting with the installed library. The correct import and usage pattern is typically: `import ifaddr; adapters = ifaddr.get_adapters()`.
Access the IP address using the `ip` attribute: `ip_object.ip` (e.g., `print(ip.ip)`).