netifaces is a Python library that provides a portable way to access network interface information, including IP addresses, netmasks, broadcast addresses, and gateway details. It supports various operating systems such as OS X, Linux, and Windows. The current version is 0.11.0, but the original project is no longer actively maintained. Forks exist to provide continued support and new features.
pip install netifacesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to list network interfaces, retrieve detailed address information for a specific interface, and fetch default gateway settings. Note that the output structure for `ifaddresses` is a dictionary keyed by system-dependent address family constants.
For ongoing development and support, consider migrating to `netifaces2` (`pip install netifaces2`) or `netifaces-plus` (`pip install netifaces-plus`). Be aware that these forks may introduce API changes.
Always use the symbolic constants provided directly by the `netifaces` module (e.g., `netifaces.AF_INET`) when querying `ifaddresses` results, rather than hardcoded integers or `socket.AF_INET`.
Iterate through the `addresses` dictionary and its lists, explicitly checking for desired address families (e.g., `if netifaces.AF_INET in addresses: ...`) and iterating through the list of addresses for that family.
Ensure that your system has the necessary build tools installed (e.g., `build-essential` on Debian/Ubuntu, Xcode Command Line Tools on macOS, or Visual C++ build tools on Windows). Preferably, use `pip install netifaces` in an environment where pre-compiled wheels are available for your Python version and OS.
If working with Windows and needing to retrieve IPv4 addresses, ensure the target network interface is active ('up'). Forks like `netifaces2` introduced `netifaces.interface_is_up()` to explicitly check interface status, which might be a necessary check if migrating or dealing with such scenarios.Install the package using pip: `pip install netifaces`. If using a specific Python version, use `python3 -m pip install netifaces` or `python -m pip install netifaces`. For Ubuntu/Debian, `sudo apt install python3-netifaces` might also be necessary.
Ensure `netifaces` is installed for the correct Python interpreter by running `pip install netifaces` (or `pip3 install netifaces` for Python 3). If multiple Python versions are present, explicitly use `python -m pip install netifaces` or `python3 -m pip install netifaces` to ensure the package is installed into the desired environment.
Download and install the 'Build Tools for Visual Studio' from the provided Microsoft link. During installation, make sure to select the C++ build tools. After installation, try `pip install netifaces` again.
Ensure `netifaces` is installed for the *exact* Python interpreter you are using. This often means reinstalling it within the specific environment (e.g., virtual environment or a specific Python installation) with `pip install --no-binary :all: netifaces` to force a fresh compilation, or verifying that the correct Python environment is active before installation.
No dependency data recorded yet.