I have Python on my Ubuntu system, but gcc can't find Python.h

128,114

Solution 1

You need the python-dev package which contains Python.h

Solution 2

On Ubuntu, you would need to install a package called python-dev. Since this package doesn't seem to be installed (locate Python.h didn't find anything) and you can't install it system-wide yourself, we need a different solution.

You can install Python in your home directory -- you don't need any special permissions to do this. If you are allowed to use a web browser and run a gcc, this should work for you. To this end

  1. Download the source tarball.

  2. Unzip with

    tar xjf Python-2.7.2.tar.bz2
    
  3. Build and install with

    cd Python-2.7.2
    ./configure --prefix=/home/username/python --enable-unicode=ucs4
    make
    make install
    

Now, you have a complete Python installation in your home directory. Pass -I /home/username/python/include to gcc when compiling to make it aware of Python.h. Pass -L /home/username/python/lib and -lpython2.7 when linking.

Solution 3

You have to use #include "python2.7/Python.h" instead of #include "Python.h".

Solution 4

For Ubuntu 15.10 and Python 3, comming to this question as they don't have Python.h but having administrative rights, the following might solve it:

sudo apt-get install python-dev
sudo apt-get install python3-dev
sudo apt-get install libpython3-dev
sudo apt-get install libpython3.4-dev
sudo apt-get install libpython3.5-dev

Solution 5

On ubuntu you can just type sudo apt-get install python-dev -y in terminal to install the python-dev package.

Share:
128,114
user979344
Author by

user979344

Updated on August 04, 2020

Comments

  • user979344
    user979344 almost 4 years

    I am on a school computer, so I can't install anything.

    I am trying to create C code which can be run in Python. It seems all the articles I am finding on it require you to use

    #include <Python.h>
    

    I do this, but when I compile it complains that there is no such file or directory.

    The computer has Python (at least it has the python command in the terminal, and we can run whatever Python code we want).

    I typed in locate Python.h in the terminal, but it found nothing.

    I have two questions:

    1. Can I write C code that I can call in Python without Python.h?

    2. Am I missing something, and the computer actually has Python.h?

  • Andrew Marsh
    Andrew Marsh over 12 years
    The questioner has stated that they are unable to install new packages on the system.
  • user979344
    user979344 over 12 years
    well, it mostly works. I am getting this error now: /home/pdem/python/Include/Python.h:8:22: error: pyconfig.h: No such file or directory....btw the computer is running python version 2.6.5...does that make a difference? thanks!
  • Sven Marnach
    Sven Marnach over 12 years
    @user979344: I don't know the reason for the error message. The capital I in your path surprise me a bit -- you should use the Python.h from where you installed Python, not from where you unpacked the tarball. The Python version that was already installed does not matter -- you are installing your own Python, any version you want. It can coexist with the system-wide installation.
  • Jesse Pepper
    Jesse Pepper over 11 years
    I have installed python-dev in ubuntu 12.04 and get a /usr/include/python2.7 area but no python.h file exists in there. Any ideas?
  • Jonathan Hartley
    Jonathan Hartley almost 11 years
    python/Lib contains the Python modules of the stdlib. I think you mean -L...python/Python/, which contains some .c and .o files.
  • Sven Marnach
    Sven Marnach almost 11 years
    @JonathanHartley: No, I meant exactly what I wrote. You are confusing the path Lib/ in the source distribution with the path $prefix/lib of the installed software.
  • Jonathan Hartley
    Jonathan Hartley almost 11 years
    @SvenMarnach aha! I see. Thanks.
  • RDK
    RDK about 10 years
    This solved it for me on a newly launched Ubuntu instance on AWS. sudo apt-get install libpython2.7-dev
  • GreenMatt
    GreenMatt over 9 years
    The OP says they're on a school computer, so sudo apt-get probably isn't available to them.
  • Werner
    Werner almost 9 years
    Some cases the package is called python-devel x)
  • Jithin Pavithran
    Jithin Pavithran over 7 years
    Try locate Python.h and see if you already have the file before you do all this. If you can find the file located, mostly this answer will work: stackoverflow.com/a/19344978/4954434 (It might be just a path issue)
  • m3nda
    m3nda about 7 years
    Once you know where and how are the libs stored that makes more sense, nevermind to get a machine-produced file with no path specified :D
  • fanny
    fanny about 7 years
    it works for ubuntu 16.04, too :) thank you, Martin!
  • Shawn Anderson
    Shawn Anderson over 6 years
    sudo apt-get install libpython3.6-dev solved it for me. Thanks!
  • DLH
    DLH about 6 years
    You saved my life. I had python-dev installed and still couldn't get it to work until I saw your answer. Up-voting your answer so more people see this.
  • nos
    nos over 5 years
    For python3, it would be python3-config
  • vineeshvs
    vineeshvs about 5 years
    Command used: "$ gcc -Wall -I /home/vineesh/python/include insertion_sort.c -nostartfiles -L /home/vineesh/python/lib -lpython2.7 -o insertion_sort ". Getting the error: "/usr/include/numpy/ndarrayobject.h:17:20: fatal error: Python.h: No such file or directory compilation terminated."
  • unlockme
    unlockme almost 5 years
    Bloody stuff, creating the symlink worked. I had tried everything else recommended without much success.
  • Jekson
    Jekson over 4 years
    sudo apt-get install libpython3.7-dev solved it for Ubuntu 18.04 and Python 3.7
  • jrh
    jrh about 4 years
    If you are the owner of the C/C++ code including Python.h this may be a reasonable solution, if you're not, you may be better off editing a build script instead to change your include directories; I think the particular library I'm trying to use never imagined that Python 3 would ever be a thing. Another thing worth mentioning, I'd say under no circumstance should you do something like symlinking the Python2 headers or doing anything else to pretend both python versions are the same... on Linux systems this can be fatal because unfortunately Linux needs both Python 2 and 3 independently.
  • johannes_lalala
    johannes_lalala almost 4 years
    see danielcooperxyz's answer: gcc $(python-config --includes)
  • N4ppeL
    N4ppeL almost 4 years
    What is the "correct" way to fix pip not finding Python.h ? I installed py3.7 on Ubuntu18, and libpython3.7-dev does not add any headers, but they exist in /usr/include/python3.6m. I could install the library with pip after creating a symlink python3.7m -> python3.6m, but as you stated, this is extremely problematic and should not be done. So what should be done instead?
  • zappfinger
    zappfinger over 3 years
    I tried all of these, on Mint 20 python3.8.5 is installed, tried that too, to no avail...
  • Klaas van Schelven
    Klaas van Schelven over 3 years
    python3-dev in current Ubuntu distributions (assuming Python 3)
  • Mateen Ulhaq
    Mateen Ulhaq almost 3 years
    Or python3.7-dev, for Python 3.7.
  • AntonK
    AntonK about 2 years
    a hint for VisualStudio users - use back-quotes around python-config --includes if you want to add this helper into Additonal Options for C/C++