Thursday, May 28, 2015

Install netifaces on OSX

As part of a bigger venture I came to need python's netifaces module installed on my OSX 10.9.5 machine. "5 seconds with pip" I thought... Several hours later I finally got it working, so if you're reading this chances are my experience may save you some time too.

First the problem: The install was failing to find stdio.h.

(venv)Macintosh:$  pip3.4 install netifaces
Collecting netifaces
  Using cached netifaces-0.10.4.tar.gz
Building wheels for collected packages: netifaces
  Running setup.py bdist_wheel for netifaces
  Complete output from command Users/Downloads/od/sfc/sfc-py/venv/bin/python3 -c "import setuptools;__file__='/private/var/folders/ty/csd27_gx4x7_l32_f1l74ggm0000gn/T/pip-build-_21m5krb/netifaces/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /var/folders/ty/csd27_gx4x7_l32_f1l74ggm0000gn/T/tmpdwdqfz57pip-wheel-:
  running bdist_wheel
  running build
  running build_ext
  checking for getifaddrs...not found.
  checking for getnameinfo...not found.
  checking for socket IOCTLs...not found.
  checking for optional header files...none found.
  checking whether struct sockaddr has a length field...no.
  checking which sockaddr_xxx structs are defined...none!
  checking for routing socket support...no.
  checking for sysctl(CTL_NET...) support...no.
  checking for netlink support...no.
  building 'netifaces' extension
  /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -g -DNETIFACES_VERSION=0.10.4 -I/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c netifaces.c -o build/temp.macosx-10.6-intel-3.4/netifaces.o
  In file included from netifaces.c:1:
  /Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m/Python.h:25:10: fatal error: 'stdio.h' file not found
  #include <stdio.h>
           ^
  1 error generated.
  error: command '/usr/bin/clang' failed with exit status 1
 
  ----------------------------------------
  Failed building wheel for netifaces
Failed to build netifaces
Installing collected packages: netifaces
  Running setup.py install for netifaces
    Complete output from command /Users/Downloads/od/sfc/sfc-py/venv/bin/python3 -c "import setuptools, tokenize;__file__='/private/var/folders/ty/csd27_gx4x7_l32_f1l74ggm0000gn/T/pip-build-_21m5krb/netifaces/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/ty/csd27_gx4x7_l32_f1l74ggm0000gn/T/pip-yktk64ig-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/Downloads/od/sfc/sfc-py/venv/bin/../include/site/python3.4/netifaces:
    running install
    running build
    running build_ext
    checking for getifaddrs...not found. (cached)
    checking for getnameinfo...not found. (cached)
    checking for socket IOCTLs...not found. (cached)
    checking for optional header files...none found. (cached)
    checking whether struct sockaddr has a length field...no. (cached)
    checking which sockaddr_xxx structs are defined...none! (cached)
    checking for routing socket support...no. (cached)
    checking for sysctl(CTL_NET...) support...no. (cached)
    checking for netlink support...no. (cached)
    building 'netifaces' extension
    /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -g -DNETIFACES_VERSION=0.10.4 -I/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c netifaces.c -o build/temp.macosx-10.6-intel-3.4/netifaces.o
    In file included from netifaces.c:1:
    /Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m/Python.h:25:10: fatal error: 'stdio.h' file not found
    #include <stdio.h>
             ^
    1 error generated.
    error: command '/usr/bin/clang' failed with exit status 1
   
    ----------------------------------------
Command "/Users/Downloads/od/sfc/sfc-py/venv/bin/python3 -c "import setuptools, tokenize;__file__='/private/var/folders/ty/csd27_gx4x7_l32_f1l74ggm0000gn/T/pip-build-_21m5krb/netifaces/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/ty/csd27_gx4x7_l32_f1l74ggm0000gn/T/pip-yktk64ig-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/Downloads/od/sfc/sfc-py/venv/bin/../include/site/python3.4/netifaces" failed with error code 1 in /private/var/folders/ty/csd27_gx4x7_l32_f1l74ggm0000gn/T/pip-build-_21m5krb/netifaces


A quick google search suggested that I install the command-line tools for xcode to get the standard stdio.h, using

xcode-select --install

 Alas that didn't solve the problem...

After numerous installs/upgades/downgrades of xcode and python3.4 i finally found the problem and solution: I had left the original OSX python install in place.

The following post tells you the few steps needed to remove it and replace it with a brew installed python:

http://stackoverflow.com/questions/22774529/what-is-the-safest-way-to-removing-python-framework-files-that-are-located-in-di

 After this I created a new virtualenv for my project, and installed netifaces besides other dependecies without a hitch. Phew...

(venv3)Macintosh:$  pip3.4 install netifaces
Collecting netifaces
  Using cached netifaces-0.10.4.tar.gz
Building wheels for collected packages: netifaces
  Running setup.py bdist_wheel for netifaces
  Stored in directory: /Users/Library/Caches/pip/wheels/32/42/e1/c70ed63c260bddfedf2472663153d03a17ee650b512273991d
Successfully built netifaces
Installing collected packages: netifaces
Successfully installed netifaces-0.10.4