Python – Solucionando o erro fatal error Python.h no such file or directory no pip install

É muito chato quando se esta desenvolvendo ou preparando o ambiente para desenvolvimento e começa a surgir erros pelo caminho, ninguém gosta né? Comigo não é e não foi diferente, quando estava preparando um ambiente para um novo projeto em Python, depois que instalei o pip e o virtualenv, criei o ambiente que precisava e comecei a instalar os pacotes, e foi quando na instalação do RPi.GPIO ocorreu um erro, vamos ver o caso passo a passo.

Configuração do ambiente host

Linux Ubuntu 14.04 LTS AMD64
Kernel 3.13.0
Python 2.7.4
Virtualenv 1.11.6
Pip 1.5.4

Depois de criar o ambiente Python, pesquisei sobre o pacote RPi.GPIO:

(proj_rpi_gpio)cleiton@vm03 ~/projetos/python/rpi/proj_rpi_gpio $ pip search RPi.GPIO
RPIO - Advanced GPIO for the Raspsberry Pi. Extends RPi.GPIO with PWM, GPIO interrupts, TCP socket interrupts, command line tools and more
RPi.GPIO - A module to control Rapsberry Pi GPIO channels

O pacote neste caso que eu queria é o segundo e segui com a etapa para instalação:

(proj_rpi_gpio)cleiton@vm03 ~/projetos/python/rpi/proj_rpi_gpio $ pip install RPi.GPIO
Downloading/unpacking rpi.gpio

Downloading RPi.GPIO-0.5.6.tar.gz

Running setup.py (path:/home/cleiton/projetos/python/rpi/proj_rpi_gpio/build/rpi.gpio/setup.py) egg_info for package rpi.gpio

Installing collected packages: rpi.gpio

Running setup.py install for rpi.gpio

building 'RPi.GPIO' extension

x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/py_gpio.c -o build/temp.linux-x86_64-2.7/source/py_gpio.o

source/py_gpio.c:23:20: fatal error: Python.h: No such file or directory

compilation terminated.

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Complete output from command /home/cleiton/projetos/python/rpi/proj_rpi_gpio/bin/python -c "import setuptools, tokenize;__file__='/home/cleiton/projetos/python/rpi/proj_rpi_gpio/build/rpi.gpio/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('rn', 'n'), __file__, 'exec'))" install --record /tmp/pip-pgk63j-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/cleiton/projetos/python/rpi/proj_rpi_gpio/include/site/python2.7:

running install

running build

running build_py

creating build

creating build/lib.linux-x86_64-2.7

creating build/lib.linux-x86_64-2.7/RPi

copying RPi/__init__.py -> build/lib.linux-x86_64-2.7/RPi

running build_ext

building 'RPi.GPIO' extension

creating build/temp.linux-x86_64-2.7

creating build/temp.linux-x86_64-2.7/source

x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/py_gpio.c -o build/temp.linux-x86_64-2.7/source/py_gpio.o

source/py_gpio.c:23:20: fatal error: Python.h: No such file or directory

compilation terminated.

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

----------------------------------------

Cleaning up...

Command /home/cleiton/projetos/python/rpi/proj_rpi_gpio/bin/python -c "import setuptools, tokenize;__file__='/home/cleiton/projetos/python/rpi/proj_rpi_gpio/build/rpi.gpio/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('rn', 'n'), __file__, 'exec'))" install --record /tmp/pip-pgk63j-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/cleiton/projetos/python/rpi/proj_rpi_gpio/include/site/python2.7 failed with error code 1 in /home/cleiton/projetos/python/rpi/proj_rpi_gpio/build/rpi.gpio

Storing debug log for failure in /home/cleiton/.pip/pip.log

Usei N vezes o pip e nunca havia visto este erro, mas era uma instalação nova do Linux, eu não estava perdido ao extremo quando vi este erro source/py_gpio.c:23:20: fatal error: Python.h: No such file or directory, entendi que o gcc (Linha 46) estava com problemas para construir a aplicação, mas como resolver?

Pesquisando achei este link que foi perfeito para solucionar, onde o que faltou era o python-dev com os arquivos headers para o gcc além de algumas ferramentas e bibliotecas para que seja possivel construir módulos Python, para instalar é muito simples.

cleiton@vm03 ~/projetos/python/rpi/proj_rpi_gpio $ sudo apt-get update && sudo apt-get install python-dev

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
...

Abstrai a saída da instalação, após o termino tentei novamente a instalação do RPi.GPIO, vamos ver o resultado.

(proj_rpi_gpio)cleiton@vm03 ~/projetos/python/rpi/proj_rpi_gpio $ pip install RPi.GPIO
Downloading/unpacking RPi.GPIO

Downloading RPi.GPIO-0.5.6.tar.gz
Running setup.py (path:/home/cleiton/projetos/python/rpi/proj_rpi_gpio/build/RPi.GPIO/setup.py) egg_info for package RPi.GPIO

Installing collected packages: RPi.GPIO
Running setup.py install for RPi.GPIO
building 'RPi.GPIO' extension
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/py_gpio.c -o build/temp.linux-x86_64-2.7/source/py_gpio.o
source/py_gpio.c: In function ‘py_cleanup’:
source/py_gpio.c:101:31: warning: ‘i’ may be used uninitialized in this function [-Wuninitialized]
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/c_gpio.c -o build/temp.linux-x86_64-2.7/source/c_gpio.o
source/c_gpio.c: In function ‘setup’:
source/c_gpio.c:71:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
source/c_gpio.c:72:34: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
source/c_gpio.c:76:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/cpuinfo.c -o build/temp.linux-x86_64-2.7/source/cpuinfo.o
source/cpuinfo.c: In function ‘get_cpuinfo_revision’:
source/cpuinfo.c:38:12: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/event_gpio.c -o build/temp.linux-x86_64-2.7/source/event_gpio.o
source/event_gpio.c: In function ‘gpio_set_edge’:
source/event_gpio.c:120:10: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
source/event_gpio.c: In function ‘gpio_set_direction’:
source/event_gpio.c:102:14: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
source/event_gpio.c:104:14: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
source/event_gpio.c: In function ‘gpio_unexport’:
source/event_gpio.c:86:10: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
source/event_gpio.c: In function ‘gpio_export’:
source/event_gpio.c:71:10: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/soft_pwm.c -o build/temp.linux-x86_64-2.7/source/soft_pwm.o
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/py_pwm.c -o build/temp.linux-x86_64-2.7/source/py_pwm.o
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/common.c -o build/temp.linux-x86_64-2.7/source/common.o
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/constants.c -o build/temp.linux-x86_64-2.7/source/constants.o
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/source/py_gpio.o build/temp.linux-x86_64-2.7/source/c_gpio.o build/temp.linux-x86_64-2.7/source/cpuinfo.o build/temp.linux-x86_64-2.7/source/event_gpio.o build/temp.linux-x86_64-2.7/source/soft_pwm.o build/temp.linux-x86_64-2.7/source/py_pwm.o build/temp.linux-x86_64-2.7/source/common.o build/temp.linux-x86_64-2.7/source/constants.o -o build/lib.linux-x86_64-2.7/RPi/GPIO.so

Successfully installed RPi.GPIO
Cleaning up...

Maravilha, pacote instalado com sucesso! Ficou facil entender e resolver o problema não?

Espero que ajude quem passar pelo mesmo problema.

Até a próxima!

Referência

http://www.cyberciti.biz/faq/debian-ubuntu-linux-python-h-file-not-found-error-solution/

Share Button

CC BY-NC-SA 4.0 Python – Solucionando o erro fatal error Python.h no such file or directory no pip install by Cleiton Bueno is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.