I have Python on my Ubuntu system, but gcc can't find Python.h
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
Download the source tarball.
-
Unzip with
tar xjf Python-2.7.2.tar.bz2
-
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.
user979344
Updated on August 04, 2020Comments
-
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:
Can I write C code that I can call in Python without
Python.h
?Am I missing something, and the computer actually has
Python.h
?
-
Andrew Marsh over 12 yearsThe questioner has stated that they are unable to install new packages on the system.
-
user979344 over 12 yearswell, 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 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 thePython.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 over 11 yearsI 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 almost 11 yearspython/Lib contains the Python modules of the stdlib. I think you mean -L...python/Python/, which contains some .c and .o files.
-
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 almost 11 years@SvenMarnach aha! I see. Thanks.
-
RDK about 10 yearsThis solved it for me on a newly launched Ubuntu instance on AWS.
sudo apt-get install libpython2.7-dev
-
GreenMatt over 9 yearsThe OP says they're on a school computer, so
sudo apt-get
probably isn't available to them. -
Werner almost 9 yearsSome cases the package is called python-devel x)
-
Jithin Pavithran over 7 yearsTry
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 about 7 yearsOnce 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 about 7 yearsit works for ubuntu 16.04, too :) thank you, Martin!
-
Shawn Anderson over 6 yearssudo apt-get install libpython3.6-dev solved it for me. Thanks!
-
DLH about 6 yearsYou 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 over 5 yearsFor python3, it would be
python3-config
-
vineeshvs about 5 yearsCommand 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 almost 5 yearsBloody stuff, creating the symlink worked. I had tried everything else recommended without much success.
-
Jekson over 4 yearssudo apt-get install libpython3.7-dev solved it for Ubuntu 18.04 and Python 3.7
-
jrh about 4 yearsIf 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 almost 4 yearssee danielcooperxyz's answer: gcc $(python-config --includes)
-
N4ppeL almost 4 yearsWhat 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 over 3 yearsI tried all of these, on Mint 20 python3.8.5 is installed, tried that too, to no avail...
-
Klaas van Schelven over 3 yearspython3-dev in current Ubuntu distributions (assuming Python 3)
-
Mateen Ulhaq almost 3 yearsOr
python3.7-dev
, for Python 3.7. -
AntonK about 2 yearsa hint for VisualStudio users - use back-quotes around
python-config --includes
if you want to add this helper into Additonal Options for C/C++