Tuesday, August 30, 2011

python: how to fix 'fatal error: Python.h: No such file or directory'

Was trying to install PyYAML. Got the latest tarball from the PyYAML website, and ran the usual:

sudo python setup.py install

which resulted in:

running install
running build
running buildpy
creating build
creating build/lib.linux-i686-2.6
creating build/lib.linux-i686-2.6/yaml
copying lib/yaml/constructor.py -> build/lib.linux-i686-2.6/yaml
.
.
.
running buildext
creating build/temp.linux-i686-2.6
checking if libyaml is compilable
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c build/temp.linux-i686-2.6/checklibyaml.c -o build/temp.linux-i686-2.6/checklibyaml.o
checking if libyaml is linkable
gcc -pthread build/temp.linux-i686-2.6/checklibyaml.o -lyaml -o build/temp.linux-i686-2.6/checklibyaml
building 'yaml' extension
creating build/temp.linux-i686-2.6/ext
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c ext/yaml.c -o build/temp.linux-i686-2.6/ext/yaml.o
ext/yaml.c:4: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1


Google quickly yielded the solution. The missing Python.h was in the python-dev package. Since I was running python2.6.6, I simply needed to do:

sudo apt-get install python2.6-dev

after which compilation and installation was successful:

running install
running build
running buildpy
running buildext
building '_yaml' extension
.
.
.
running installegginfo
Writing /usr/local/lib/python2.6/dist-packages/PyYAML-3.10.egg-info

No comments: