33

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,

3
  • 3
    Search for how to install python-dev on OSX. Commented Mar 3, 2016 at 17:08
  • Did all of it - XCode command line, Brew, python, virtualenv. Also set PATH in bash_profile. Am I missing something? Commented Mar 3, 2016 at 17:22
  • 2
    You need to set C_INCLUDE_PATH try this -> stackoverflow.com/a/47956013/4502723 Commented Dec 23, 2017 at 21:10

11 Answers 11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

export C_INCLUDE_PATH=/System/Library/Frameworks/Python.framework/Versions/2.7/Headers/ worked for me
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.
16

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.

3 Comments

CPLUS_INCLUDE_PATH is used in the accepted answer, though neither worked for me
MacOS — C_INCLUDE_PATH works, CPLUS... — doesn't
C_INCLUDE_PATH was the one that worked for me, CPLUS_INCLUDE_PATH didn't. For reference, on an M1 Mac trying to build the wheel for Greenlet.
9

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.

3 Comments

how to add it to cmake? I am still not able to build
@ShravyaBoggarapu sorry but this post is from long time ago. I think you should look into CMake FindPython. For example here.
To add to the Homebrew variant - Apple silicon Macs use /opt/homebrew/Cellar instead as the path - you can find out by brew --cellar.
7

Run

brew install python

or

brew upgrade python

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

1 Comment

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
4

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

3 Comments

I spent a few hours on this error, but this answer solved it for me!
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/[email protected]/3.8.6/Frameworks/Python.framework/Versions/3.8/include/python3.8 instead.
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".
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 ..

1 Comment

Thank you, it works also in my case where my virtual env is provided with pyenv
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.

Comments

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.

Comments

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.