Sumanth
Sumanth

Reputation: 331

fatal error: 'Python.h' file not found while installing opencv

I am trying to get opencv 3.1 installed for Python on my Mac OS X 10.10.5 I'm following the steps as outlined here - http://www.pyimagesearch.com/2015/06/15/install-opencv-3-0-and-python-2-7-on-osx/

When I actually try installing opencv after all the setup, I get the following error:

.../opencv/modules/python/src2/cv2.cpp:6:10: fatal error: 
  'Python.h' file not found
 #include <Python.h>
          ^

I looked around StackOverflow and found that most people facing this issue are using Anaconda, which is not my case. It would be great if someone could point me in the right direction to get this fixed.

Thanks,

Upvotes: 33

Views: 57741

Answers (11)

alexpirogovski
alexpirogovski

Reputation: 1

In my case (I tried to install ruamel.yaml.clib via requirements file) the failed command was:

clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -Wno-error=unreachable-code -I/Users/<MY_USER>/git/<SOME_PATH>/venv/include -I/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Headers -c _ruamel_yaml.c -o build/temp.macosx-10.9-universal2-cpython-39/_ruamel_yaml.o

And it failed at:

      _ruamel_yaml.c:6:10: fatal error: 'Python.h' file not found

So looking at the command I've figured I need to install Xcode. Not the xcode-select but the Xcode application from the AppStore: https://apps.apple.com/us/app/xcode/id497799835?mt=12

Upvotes: 0

Foad S. Farimani
Foad S. Farimani

Reputation: 14016

This question seems to be regarding the default Python2 integrated into the macOS, for which the Python.h header file is in the address:

/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/Python.h

the Python version might change depending on the macOS version you are using though. However, if you have installed Python3 using Hombrew, as probably you should then you might find the header file in a path like:

/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/include/python3.7m/Python.h

depending on the version of Python3 your brew has installed for you. Again there are many other possibilities to get Python on mac (e.g., anaconda, intel python, pypy,... you name it). The best way to find the path to a specific Python.h is to search your entire device with:

sudo find / -iname "Python.h"

Then you can run the command

export C_INCLUDE_PATH="<path/to/the/specific/header/folder>"

in your bash terminal, or add it to the ~/.bash_profile to have it there permanently.

You may wanna also check the MakeFile (or other tooling the software uses e.g., cmake...) to see what versions of Python.h it is expecting and in what locations. it is expecting it.

Upvotes: 9

leach
leach

Reputation: 225

I solved this problem on mac os 12.2.1 like this: export CPPFLAGS="-I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7" and then pip install ...

Upvotes: 4

Younes Nj
Younes Nj

Reputation: 596

I fixed my problem by installing python2.7, apparently it was compiling using python 3.4. So I did the following:

brew install python@2
brew link python@2

Upvotes: 0

Feuda
Feuda

Reputation: 2365

Run

brew install python

or

brew upgrade python

after doing this, everything(vim in my case) you want to install will be success.

Upvotes: 7

H. Jiang
H. Jiang

Reputation: 131

My approach was different, but it's basically what Jonathan Lau mentioned.

I used pyenv and conda and changed my python lib path which caused the problem. To solve it, here's what I did

  1. Commented out PATH setting for pyenv in .bashrc
  2. Restart terminal and brew install whatever you need (vim in my case)
  3. Change .bashrc back

Upvotes: 0

yoziru
yoziru

Reputation: 31

I had the same problem on OSX, fixed by setting the CPLUS_INCLUDE_PATH environment variable. Was also building in an anaconda environment which might have complicated things.

export CPLUS_INCLUDE_PATH=~/anaconda/envs/py27/include/python2.7

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=~/anaconda/envs/py27/share \
    -D PYTHON2_PACKAGES_PATH=~/anaconda/envs/py27/lib/python2.7/site-packages \
    -D PYTHON2_LIBRARY=~/anaconda/envs/py27/bin/python \
    -D PYTHON_EXECUTABLE=~/anaconda/envs/py27/bin/python \
    -D PYTHON2_INCLUDE_DIR=~/anaconda/envs/py27/include/python2.7 \
    -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON \
    -D BUILD_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..

Upvotes: 3

Sergey Sargsyan
Sergey Sargsyan

Reputation: 452

If Python.h is not found while making one of the *.cpp files, set the following ENV variable

export CPLUS_INCLUDE_PATH=/System/Library/Frameworks/Python.framework/Headers

Please check the existence of the path in your system and make sure that Python.h is there.

Upvotes: 25

VICENTE GONZALEZ RUIZ
VICENTE GONZALEZ RUIZ

Reputation: 79

Yes, revise the paths used in the cmake command. They must exist in your filesystem. In my case, I have installed python 3.5 and the original documentation uses python 3.4.

Upvotes: 0

Jonathan Lau
Jonathan Lau

Reputation: 101

First, you have to check and make sure you have installed the python using brew and you are using the system python lib binary. That's was mentioned in the blog.

Second, the python version in the cmake command must match what brew has installed for you. You should double check that.

Upvotes: 1

polarise
polarise

Reputation: 2413

I'm using El Capitan but I don't think there should be much difference in the path to the Python header. I find mine at:

/System/Library/Frameworks/Python.framework/Headers/Python.h

You could try and run:

export C_INCLUDE_PATH=/System/Library/Frameworks/Python.framework/Headers

then try the remaining steps.

Upvotes: 16

Related Questions