Wednesday, April 22, 2020

Python fatal error: Python.h: No such file or directory

I got this error while installing a Python package



Installing collected packages: psutil, wcwidth, pyte, thefuck           [37/257]
  Running setup.py install for psutil ... error
    Complete output from command /usr/bin/python3 -u -c "import setuptools, toke
nize;__file__='/tmp/pip-build-2lqls7pu/psutil/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(c
ode, __file__, 'exec'))" install --record /tmp/pip-kips6a90-record/install-recor
d.txt --single-version-externally-managed --compile --user --prefix=:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/psutil
    copying psutil/_pssunos.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/_common.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/_psposix.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/_pslinux.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/_psaix.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/_psosx.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/_pswindows.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/_psbsd.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/__init__.py -> build/lib.linux-x86_64-3.6/psutil
    copying psutil/_compat.py -> build/lib.linux-x86_64-3.6/psutil
    creating build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_system.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_memory_leaks.py -> build/lib.linux-x86_64-3.6/psut
il/tests
    copying psutil/tests/test_linux.py -> build/lib.linux-x86_64-3.6/psutil/test
s
    copying psutil/tests/test_sunos.py -> build/lib.linux-x86_64-3.6/psutil/test
s
    copying psutil/tests/test_misc.py -> build/lib.linux-x86_64-3.6/psutil/tests    copying psutil/tests/test_aix.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_windows.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_osx.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/__main__.py -> build/lib.linux-x86_64-3.6/psutil/tests

    copying psutil/tests/__main__.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_process.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_connections.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_bsd.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/runner.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/__init__.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_unicode.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_posix.py -> build/lib.linux-x86_64-3.6/psutil/tests
    copying psutil/tests/test_contracts.py -> build/lib.linux-x86_64-3.6/psutil/tests
    running build_ext
    building 'psutil._psutil_linux' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/psutil
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=570 -DPSUTIL_LINUX=1 -I/usr/include/python3.6m -c psutil/_psutil_common.c -o build/temp.linux-x86_64-3.6/psutil/_psutil_common.o
    psutil/_psutil_common.c:9:20: fatal error: Python.h: No such file or directory
     #include <Python.h>
                        ^
    compilation terminated.
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-2lqls7pu/psutil/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-kips6a90-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-build-2lqls7pu/psutil/





Python Header Files

The error message reporting missing Python.h file is because Python cannot find the header/development files. Install the necessary OS package.

For Debian or Ubuntu based OS


sudo apt-get install python-dev  
sudo apt-get install python3-dev




For CentOS / Fedora / RedHat based OS


sudo yum install python-devel  

sudo yum install python3-devel  




For example, these are the packages installed on CentOS 7


================================================================================
 Package                     Arch        Version           Repository      Size
================================================================================
Installing:
 python3-devel               x86_64      3.6.8-10.el7      xlnx-base      215 k
Installing for dependencies:
 python-rpm-macros           noarch      3-32.el7          xlnx-base      8.8 k
 python3-rpm-generators      noarch      6-2.el7           xlnx-base       20 k
 python3-rpm-macros          noarch      3-32.el7          xlnx-base      7.7 k






The Python package can now be built and installed successfully


$ pip3 install --user thefuck
Collecting thefuck
  Using cached https://files.pythonhosted.org/packages/57/56/378c08c58453d245435acdb58729db0762537c5d223bbf093454f7834c86/thefuck-3.30-py2.py3-none-any.whl
Requirement already satisfied: decorator in ./.local/lib/python3.6/site-packages (from thefuck)
Requirement already satisfied: colorama in ./.local/lib/python3.6/site-packages (from thefuck)
Collecting pyte (from thefuck)
Collecting psutil (from thefuck)
  Using cached https://files.pythonhosted.org/packages/c4/b8/3512f0e93e0db23a71d82485ba256071ebef99b227351f0f5540f744af41/psutil-5.7.0.tar.gz
Requirement already satisfied: six in ./.local/lib/python3.6/site-packages (from thefuck)
Collecting wcwidth (from pyte->thefuck)
  Using cached https://files.pythonhosted.org/packages/f6/d5/1ecdac957e3ea12c1b319fcdee8b6917ffaff8b4644d673c4d72d2f20b49/wcwidth-0.1.9-py2.py3-none-any.whl
Building wheels for collected packages: psutil
  Running setup.py bdist_wheel for psutil ... done
  Stored in directory: /home/hanxue/.cache/pip/wheels/d7/69/b4/3200b95828d1f0ddb3cb5699083717f4fdbd9b4223d0644c57
Successfully built psutil
Installing collected packages: wcwidth, pyte, psutil, thefuck
Successfully installed psutil-5.7.0 pyte-0.8.0 thefuck-3.30 wcwidth-0.1.9







No comments:

Post a Comment