Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 350
user979344 Avatar asked Nov 26 '11 22:11

user979344


People also ask

How do I fix python H No such file or directory?

You encounter "Python. h: No such file or directory" error while trying to build a shared library using the file extension of another language ( e.g. 'C' ). If you are trying to build a shared library using the file extension of another language, you need to install the correct development version of Python.

Where is python path in Ubuntu?

If you are using the standard flavour of Linux, open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin/python” and press Enter. If you have access to either sh or ksh shell, then open up the terminal and type the following, PATH=”$PATH:/usr/local/bin/python” and press Enter.

Is python already installed on Ubuntu?

Running Python in UbuntuPython comes preinstalled on almost every Linux system and is available on official distribution repositories as well. If you still don't have Python installed on your computer, you can easily download it using Ubuntu's package manager.

What is python H?

Python.h is nothing but a header file. It is used by gcc to build applications. You need to install a package called python-dev. This package includes header files, a static library and development tools for building Python modules, extending the Python interpreter or embedding Python in applications.


2 Answers

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

like image 64
Andrew Marsh Avatar answered Sep 23 '22 13:09

Andrew Marsh


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.

like image 27
Sven Marnach Avatar answered Sep 20 '22 13:09

Sven Marnach