70

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user979344
  • 842
  • 1
  • 7
  • 8
  • Call someone and ask them to download it and email it to you. Then place it wherever you want on the machine. – Hunter McMillen Nov 26 '11 at 22:57
  • 6
    @HunterMcMillen: That won't solve much, as Python.h includes a bunch of other files. Also, you need the exact Python.h that matches the system's Python version. – Petr Viktorin Nov 26 '11 at 23:02
  • 2
    Although this question is older than that, but that question got better answers. [fatal error: Python.h: No such file or directory](https://stackoverflow.com/questions/21530577/fatal-error-python-h-no-such-file-or-directory) – 273K Feb 06 '21 at 20:34

15 Answers15

71

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

Andrew Marsh
  • 2,032
  • 15
  • 14
48

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.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 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! – user979344 Nov 26 '11 at 23:15
  • @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. – Sven Marnach Nov 27 '11 at 00:10
  • python/Lib contains the Python modules of the stdlib. I think you mean -L...python/Python/, which contains some .c and .o files. – Jonathan Hartley Jul 21 '13 at 08:14
  • 2
    @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. – Sven Marnach Jul 24 '13 at 10:03
  • 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: http://stackoverflow.com/a/19344978/4954434 (It might be just a path issue) – Jithin Pavithran Jan 25 '17 at 16:42
  • 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." – vineeshvs Apr 02 '19 at 08:29
46

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

Kuchhu
  • 493
  • 4
  • 2
  • 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 – m3nda Mar 29 '17 at 01:25
  • 1
    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. – DLH May 22 '18 at 20:04
  • 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. – jrh Mar 20 '20 at 13:37
  • 1
    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? – N4ppeL Aug 18 '20 at 14:00
39

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
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
17

Generally on ubuntu you can install the python-dev package to resolve this.

Type the following in terminal to install the python-dev package.

sudo apt-get install python-dev -y
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
danielcooperxyz
  • 960
  • 1
  • 13
  • 28
14

I found the answer in ubuntuforums, you can just add this to your gcc $(python-config --includes)

gcc $(python-config --includes) urfile.c
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
7

You need python-dev installed.
For Ubuntu:

sudo apt-get install python-dev   # for python2.x installs
sudo apt-get install python3-dev  # for python3.x installs

For more distros, refer -
https://stackoverflow.com/a/21530768/6841045

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
vedipen
  • 106
  • 1
  • 2
6

The header files are now provided by libpython2.7-dev.

You can use the search form at packages.ubuntu.com to find out what package provides Python.h.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • This solved it for me on a newly launched Ubuntu instance on AWS. `sudo apt-get install libpython2.7-dev` – RDK May 21 '14 at 19:31
4

I ran into the same issue while trying to build a very old copy of omniORB on a CentOS 7 machine. Resolved the issue by installing the python development libraries:

# yum install python-devel

This installed the Python.h into:

/usr/include/python2.7/Python.h

user3892260
  • 965
  • 2
  • 10
  • 19
3

It happens because Python.h is not located in the default include folder (which is /usr/include/).

Installing Python-dev might help:

$ sudo apt-get install python-dev 

But mostly the problem will persist because the development packages are made inside a separate folder inside the include folder itself ( /usr/include/python2.7 or python3).

So you should either specify the library folder using -I option in gcc or by creating soft-links to everything inside those folders to just outside (I'd prefer the former option).

Using -I option in gcc:

$ gcc -o hello -I /usr/include/python2.7 helloworld.c

Creating soft-links :

$ sudo ln -sv /usr/include/python2.7/* /usr/include/
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
PunyCode
  • 373
  • 4
  • 18
  • Bloody stuff, creating the symlink worked. I had tried everything else recommended without much success. – unlockme Jul 25 '19 at 08:07
2
locate Python.h

If the output is empty, then find your python version

python --version

lets say it is X.x i.e 2.7 or 3.6, 3.7, 3.8 Then with the same version install header files and static libraries for python

sudo apt-get install pythonX.x-dev

GraphicalDot
  • 2,644
  • 2
  • 28
  • 43
1

That means you are not install libraries for python dev.

If you are on Linux OS, you can solve this issue by commands separately below:

  • Ubuntu (Debian) :

    sudo apt-get install python-dev (Py2) or sudo apt-get install python3-dev (Py3)

  • Rehat (CentOS):

    yum install python-devel

Little Roys
  • 5,383
  • 3
  • 30
  • 28
1

None of the answers worked for me. If you are running on Ubuntu, you can try:

With python3:

sudo apt-get install python3 python-dev python3-dev \
     build-essential libssl-dev libffi-dev \
     libxml2-dev libxslt1-dev zlib1g-dev \
     python-pip

With Python 2:

sudo apt-get install python-dev  \
     build-essential libssl-dev libffi-dev \
     libxml2-dev libxslt1-dev zlib1g-dev \
     python-pip
CanciuCostin
  • 1,773
  • 1
  • 10
  • 25
1

I think the correct way is python3-config --include, and if you look at it cat $(which python3-config), you'll see that it is using sysconfig module under the hood. Thus, I think the best solution is to use:

>>> import sysconfig
>>> sysconfig.get_path('include')
Jason Eveleth
  • 139
  • 1
  • 5
1

Go to Synaptic package manager. Reload -> Search for python -> select the python package you want -> Submit -> Install Works for me ;)

Exactly, the package you need to install is python-dev.

thathashd
  • 1,022
  • 4
  • 17
  • 49