How to get Network Interface Card names in Python?

Since this answer pops up in Google when I search for this information, I thought I should add my technique for getting the available interfaces (as well as IP addresses). The very nice module netifaces takes care of that, in a portable manner.


On Linux, you can just list the links in /sys/class/net/ by

os.listdir('/sys/class/net/')

Not sure if this works on all distributions.


A great Python library I have used to do this is psutil. It can be used on Linux, Windows, and OSX among other platforms and is supported from Python 2.6 to 3.6.

Psutil provides the net_if_addrs() function which returns a dictionary where keys are the NIC names and value is a list of named tuples for each address assigned to the NIC which include the address family, NIC address, netmask, broadcast address, and destination address.

A simple example using net_if_addrs() which will print a Python list of the NIC names:

import psutil

addrs = psutil.net_if_addrs()
print(addrs.keys())