Take the 2-minute tour ×

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?

share|improve this question
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
@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

7 Answers 7

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 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.

share|improve this answer
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 0: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 8:14
@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
@SvenMarnach aha! I see. Thanks. –  Jonathan Hartley Jul 24 '13 at 10:12

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

share|improve this answer
You are correct, must not have read it properly –  Andrew Marsh Nov 26 '11 at 22:55
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? –  Jesse Pepper Oct 3 '12 at 7:31
you saved my life –  oiyio Dec 4 at 12:26

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

share|improve this answer

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.

share|improve this answer
This solved it for me on a newly launched Ubuntu instance on AWS. sudo apt-get install libpython2.7-dev –  RDK May 21 at 19:31

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

share|improve this answer

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.

share|improve this answer
The questioner has stated that they are unable to install new packages on the system. –  Andrew Marsh Nov 26 '11 at 22:59

Currently supplied python-dev package for python2.7 contains no .h files at all. So seems installing python from sources is the only solution for me (I'm using Ubuntu 12.04)

share|improve this answer

Your Answer


