致命错误:安装 opencv 时未找到“Python.h”文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35778495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fatal error: 'Python.h' file not found while installing opencv
提问by Sumanth
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/
我正在尝试在 Mac OS X 10.10.5 上为 Python 安装 opencv 3.1 我正在按照此处概述的步骤操作 - 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 时,出现以下错误:
.../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.
我环顾了 StackOverflow,发现大多数面临这个问题的人都在使用 Anaconda,这不是我的情况。如果有人能指出我正确的方向来解决这个问题,那就太好了。
Thanks,
谢谢,
回答by Sergey Sargsyan
If Python.h is not found while making one of the *.cpp files, set the following ENV variable
如果在制作 *.cpp 文件之一时未找到 Python.h,请设置以下 ENV 变量
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.
请检查系统中路径是否存在,并确保存在 Python.h。
回答by polarise
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:
我正在使用,El Capitan
但我认为 Python 标头的路径应该没有太大区别。我在以下位置找到我的:
/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.
然后尝试剩余的步骤。
回答by Feuda
Run
跑
brew install python
or
或者
brew upgrade python
after doing this, everything(vim in my case) you want to install will be success.
执行此操作后,您要安装的所有内容(在我的情况下为 vim)都会成功。
回答by yoziru-desu
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.
我在 OSX 上遇到了同样的问题,通过设置 CPLUS_INCLUDE_PATH 环境变量来解决。还在可能有复杂事情的 anaconda 环境中构建。
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 ..
回答by Jonathan Lau
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.
首先,您必须检查并确保您已经使用 brew 安装了 python 并且您正在使用系统 python lib 二进制文件。这是博客里提到的。
Second, the python version in the cmake command must match what brew has installed for you. You should double check that.
其次,cmake 命令中的 python 版本必须与 brew 为您安装的版本相匹配。你应该仔细检查一下。
回答by VICENTE GONZALEZ RUIZ
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.
是的,修改 cmake 命令中使用的路径。它们必须存在于您的文件系统中。就我而言,我安装了 python 3.5,原始文档使用 python 3.4。
回答by H. Jiang
My approach was different, but it's basically what Jonathan Lau mentioned.
我的方法不同,但基本上就是Jonathan Lau提到的。
I used pyenv and conda and changed my python lib path which caused the problem. To solve it, here's what I did
我使用了 pyenv 和 conda 并更改了导致问题的 python lib 路径。为了解决它,这就是我所做的
- Commented out PATH setting for pyenv in .bashrc
- Restart terminal and brew install whatever you need (vim in my case)
- Change .bashrc back
- 在 .bashrc 中注释掉 pyenv 的 PATH 设置
- 重新启动终端并 brew install 你需要的任何东西(在我的例子中是 vim)
- 将 .bashrc 改回
回答by Younes Nj
I fixed my problem by installing python2.7, apparently it was compiling using python 3.4. So I did the following:
我通过安装 python2.7 解决了我的问题,显然它是使用 python 3.4 编译的。所以我做了以下事情:
brew install python@2
brew link python@2
回答by Foad
This question seems to be regarding the default Python2 integrated into the macOS, for which the Python.h
header file is in the address:
这个问题似乎是关于集成到macOS的默认Python2,其Python.h
头文件在地址中:
/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:
Python 版本可能会根据您使用的 macOS 版本而变化。但是,如果您使用 Hombrew 安装了 Python3,那么您可能应该在以下路径中找到头文件:
/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:
取决于您brew
为您安装的 Python3 版本。同样,在 mac 上使用 Python 有很多其他的可能性(例如,anaconda、intel python、pypy,...你能想到的)。找到特定路径的最佳方法Python.h
是使用以下命令搜索整个设备:
sudo find / -iname "Python.h"
Then you can run the command
然后你可以运行命令
export C_INCLUDE_PATH=<path/to/the/specific/header/file>
in your bash terminal, or add it to the ~/.bash_profile
to have it there permanently.
在您的 bash 终端中,或将其添加到~/.bash_profile
以使其永久存在。
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.
您可能还想检查 MakeFile(或软件使用的其他工具,例如 cmake...)以查看Python.h
它的预期版本和位置。它正在期待它。