30

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,

Sumanth
  • 301
  • 1
  • 3
  • 4

11 Answers11

25

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.

Sergey Sargsyan
  • 452
  • 6
  • 10
  • 1
    `export C_INCLUDE_PATH=/System/Library/Frameworks/Python.framework/Versions/2.7/Headers/` worked for me – Jeff Bezos Nov 10 '22 at 15:55
  • A couple of useful notes here for when I invariably hit this again: 1. Setting the `PYTHON_DEFAULT_EXECUTABLE` to the version of python of interest is useful 2. Similarly it may be helpful to set, `PYTHONPATH`, `PYTHON_INCLUDE_DIR` to that python's include dirs 3. Use `CPLUS_INCLUDE_PATH` + `C_INCLUDE_PATH` in each case separating multiple dirs with `:` on macos (not sure about other os's) if you need to include other headers such as `numpy`. Finally, don't forget to `-v` the install command to get build output. – toofarsideways Mar 11 '23 at 18:15
15

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.

polarise
  • 2,303
  • 1
  • 19
  • 28
8

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.

Jemshit
  • 9,501
  • 5
  • 69
  • 106
Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
7

Run

brew install python

or

brew upgrade python

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

Feuda
  • 2,335
  • 30
  • 28
  • This was the clue I needed. I had python 3.8 installed outside brew for some reason. Installing with brew and using that as the base for my virtualenv fixed my woes – Jonas D Nov 04 '22 at 15:26
3

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 ..
yoziru
  • 31
  • 1
3

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

leach
  • 185
  • 8
  • 2
    I spent a few hours on this error, but this answer solved it for me! – Saransh Oct 21 '22 at 19:17
  • I would have thought an active `venv` would include or link to the needed python header files but didn't. Those were found in `/usr/local/Cellar/python@3.8/3.8.6/Frameworks/Python.framework/Versions/3.8/include/python3.8` instead. – eliangius Nov 19 '22 at 16:30
  • 1
    Thanks, that solved it for me! For those using `pyenv` you can get the location of your headers via `pyenv prefix` + append the remaining path. In my case I had to set `export CPPFLAGS="-I$HOME/.pyenv/versions/3.9.16/include/python3.9"`. – Stan Jan 13 '23 at 13:03
1

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.

Jonathan Lau
  • 101
  • 1
  • 4
0

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.

0

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
H. Jiang
  • 121
  • 1
  • 5
0

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
Younes Nj
  • 566
  • 8
  • 16
0

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