[python] 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?

This question is related to python c ubuntu header

The answer is


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


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


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


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

gcc $(python-config --includes) urfile.c

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


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


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.


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

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


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

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/

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


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.


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.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to ubuntu

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found No 'Access-Control-Allow-Origin' header in Angular 2 app How to add header row to a pandas DataFrame How can I make sticky headers in RecyclerView? (Without external lib) Adding header to all request with Retrofit 2 Python Pandas Replacing Header with Top Row Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers Pythonically add header to a csv file fatal error C1010 - "stdafx.h" in Visual Studio how can this be corrected? correct PHP headers for pdf file download How to fix a header on scroll