Portable Network Interface Information (Fork)
netifaces2 is a Python library providing portable access to network interface information. It is a fork of the original `netifaces` library, offering improved support, particularly for Windows, and is actively maintained. The current version is 0.0.22, with recent updates focusing on stability and new features like listing interfaces by name and index, enhancing Windows compatibility.
Common errors
-
ModuleNotFoundError: No module named 'netifaces2'
cause The netifaces2 package is not installed in the active Python environment.fixRun `pip install netifaces2` in your active Python environment. -
AttributeError: module 'netifaces' has no attribute 'interfaces'
cause You intended to use `netifaces2` but imported the original `netifaces` package, which might not be installed or has an incompatible API.fixEnsure you have `netifaces2` installed (`pip install netifaces2`) and are importing it correctly: `import netifaces2`. -
KeyError: 2 (or other address family constants like 17 or 18) when accessing `addrs[netifaces2.AF_INET]`
cause The specified network interface does not have an address configured for the requested address family (e.g., no IPv4 address for AF_INET).fixAlways check if the address family exists in the returned dictionary before accessing it, e.g., `if netifaces2.AF_INET in addrs:`. -
Memory allocation issues on Windows (application uses excessive RAM or crashes)
cause Older versions of netifaces2 on Windows had known memory management issues, particularly with complex network configurations.fixUpgrade to netifaces2 version 0.0.22 or newer (`pip install --upgrade netifaces2`) which includes several Windows memory fixes.
Warnings
- gotcha Users migrating from the original `netifaces` library might mistakenly install or import `netifaces` instead of `netifaces2`. These are distinct packages, and `netifaces2` is the actively maintained fork.
- gotcha On Windows, prior to version 0.0.22, users might encounter stability issues, including potential unbounded memory allocations, particularly on complex network setups.
- gotcha Network interface names can vary significantly across operating systems (e.g., 'eth0', 'en0', 'Local Area Connection'). Hardcoding interface names can lead to non-portable code.
Install
-
pip install netifaces2
Imports
- interfaces
import netifaces netifaces.interfaces()
import netifaces2 netifaces2.interfaces()
- ifaddresses
import netifaces netifaces.ifaddresses('eth0')import netifaces2 netifaces2.ifaddresses('eth0') - AF_INET
from netifaces import AF_INET
from netifaces2 import AF_INET
Quickstart
import netifaces2
print(f"All interfaces: {netifaces2.interfaces()}")
for i in netifaces2.interfaces():
print(f"\nInterface: {i}")
addrs = netifaces2.ifaddresses(i)
if netifaces2.AF_INET in addrs:
for link in addrs[netifaces2.AF_INET]:
print(f" IPv4: {link.get('addr')}")
if netifaces2.AF_INET6 in addrs:
for link in addrs[netifaces2.AF_INET6]:
print(f" IPv6: {link.get('addr')}")
if netifaces2.AF_LINK in addrs:
for link in addrs[netifaces2.AF_LINK]:
print(f" MAC: {link.get('addr')}")