Cmake 无法找到 Python 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24174394/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:08:27  来源:igfitidea点击:

Cmake is not able to find Python-libraries

pythonpython-2.7cmake

提问by Amit Pal

Getting this error:

收到此错误:

sudo: unable to resolve host coderw@ll
-- Could NOT find PythonLibs (missing:  PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) 
CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:108     
(message):
Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE)
Call Stack (most recent call first):
/usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:315  
(_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-2.8/Modules/FindPythonInterp.cmake:139 
(FIND_PACKAGE_HANDLE_STANDARD_ARGS)
Code/cmake/Modules/FindNumPy.cmake:10 (find_package)
CMakeLists.txt:114 (find_package)



-- Configuring incomplete, errors occurred!
See also "/home/coderwall/Desktop/rdkit/build/CMakeFiles/CMakeOutput.log".
See also "/home/coderwall/Desktop/rdkit/build/CMakeFiles/CMakeError.log".

I have already installed:

我已经安装了:

  1. sudo apt-get install python-dev
  2. Environment variable are already set as follow:

    PYTHON_INCLUDE_DIRS=/usr/include/python2.7 
    PYTHON_LIBRARIES=/usr/lib/python2.7/config/libpython2.7.so
    
  1. 须藤 apt-get 安装 python-dev
  2. 环境变量已经设置如下:

    PYTHON_INCLUDE_DIRS=/usr/include/python2.7 
    PYTHON_LIBRARIES=/usr/lib/python2.7/config/libpython2.7.so
    

Location of python.h: /usr/lib/include/python2.7/python.h

地点python.h/usr/lib/include/python2.7/python.h

Location of pythonlibs: /usr/lib/python2.7/How to solve this?

pythonlibs的位置:/usr/lib/python2.7/如何解决这个问题?

回答by Patrizio Bertoni

Some last version of Ubuntu installs Python 3.4 by default and the CMake version from Ubuntu (2.8) only searches up to Python 3.3.

某些最新版本的 Ubuntu 默认安装 Python 3.4,而来自 Ubuntu (2.8) 的 CMake 版本仅搜索 Python 3.3。

Try to add set(Python_ADDITIONAL_VERSIONS 3.4)before the find_packagestatement.

尝试set(Python_ADDITIONAL_VERSIONS 3.4)find_package语句前添加。

Remember to clean CMakeCache.txttoo.

CMakeCache.txt也要记得清洁。

回答by Oleksandr Mosur

For me was helpful next:

接下来对我很有帮助:

> apt-get install python-dev python3-dev

回答by Chris Lyon

I hit the same issue,and discovered the error message gives misleading variable names. Try setting the following (singular instead of plural):

我遇到了同样的问题,发现错误消息给出了误导性的变量名称。尝试设置以下内容(单数而不是复数):

PYTHON_INCLUDE_DIR=/usr/include/python2.7 
PYTHON_LIBRARY=/usr/lib/python2.7/config/libpython2.7.so

The (plural) variables you see error messages about are values that the PythonLibs sets up when it is initialised correctly.

您看到的错误消息的(复数)变量是 PythonLibs 在正确初始化时设置的值。

回答by pdpcosta

I was facing this problem while trying to compile OpenCV 3 on a Xubuntu 14.04 Thrusty Tahr system. With all the dev packages of Python installed, the configuration process was always returning the message:

我在尝试在 Xubuntu 14.04 Thrusty Tahr 系统上编译 OpenCV 3 时遇到了这个问题。安装了 Python 的所有开发包后,配置过程总是返回消息:

Could NOT found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.6", minimum required is "2.7")
Could NOT find PythonLibs (missing: PYTHON_INCLUDE_DIRS) (found suitable exact version "2.7.6")
Found PythonInterp: /usr/bin/python3.4 (found suitable version "3.4", minimum required is "3.4")
Could NOT find PythonLibs (missing: PYTHON_LIBRARIES) (Required is exact version "3.4.0")

The CMake version available on Thrusty Tahr repositories is 2.8. Some posts inspired me to upgrade CMake. I've added a PPA CMake repository which installs CMake version 3.2.

Thrusty Tahr 存储库上可用的 CMake 版本是 2.8。一些帖子激励我升级 CMake。我添加了一个安装 CMake 3.2 版的 PPA CMake 存储库。

After the upgrade everything ran smoothly and the compilation was successful.

升级后一切顺利,编译成功。

回答by xenon

This problem can also happen in Windows. Cmake looks into the registry and sometimes python values are not set. For those with similar problem:

此问题也可能发生在 Windows 中。Cmake 查看注册表,有时未设置 python 值。对于有类似问题的人:

http://ericsilva.org/2012/10/11/restoring-your-python-registry-in-windows/

http://ericsilva.org/2012/10/11/restoring-your-python-registry-in-windows/

Just create a .reg file to set the necessary keys and edit accordingly to match your setup.

只需创建一个 .reg 文件来设置必要的键并进行相应的编辑以匹配您的设置。

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Python]

[HKEY_CURRENT_USER\Software\Python\Pythoncore]

[HKEY_CURRENT_USER\Software\Python\Pythoncore.6]

[HKEY_CURRENT_USER\Software\Python\Pythoncore.6\InstallPath]
@="C:\python26"

[HKEY_CURRENT_USER\Software\Python\Pythoncore.6\PythonPath]
@="C:\python26;C:\python26\Lib\;C:\python26\DLLs\"

[HKEY_CURRENT_USER\Software\Python\Pythoncore.7]

[HKEY_CURRENT_USER\Software\Python\Pythoncore.7\InstallPath]
@="C:\python27"

[HKEY_CURRENT_USER\Software\Python\Pythoncore.7\PythonPath]
@="C:\python27;C:\python27\Lib\;C:\python27\DLLs\"

回答by Ivan De Paz Centeno

You can fix the errors by appending to the cmakecommand the -DPYTHON_LIBRARYand -DPYTHON_INCLUDE_DIRflags filled with the respective folders.

您可以通过将填充了相应文件夹cmake-DPYTHON_LIBRARY-DPYTHON_INCLUDE_DIR标志附加到命令来修复错误。

Thus, the trick is to fill those parameters with the returned information from the python interpreter, which is the most reliable. This may work independently of your python location/version (also for Anaconda users):

因此,诀窍是用 Python 解释器返回的信息填充这些参数,这是最可靠的。这可能独立于您的 python 位置/版本(也适用于 Anaconda 用户):

$ cmake .. \
-DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")  \
-DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))")

If the version of python that you want to link against cmake is Python3.X and the default python symlink points to Python2.X, python3 -c ...can be used instead of python -c ....

如果要针对 cmake 链接的 python 版本是 Python3.X 并且默认的 python 符号链接指向 Python2.X,python3 -c ...则可以使用python -c ....

In case that the error persists, you may need to update the cmaketo a higher version as stated by @pdpcosta and repeat the process again.

如果错误仍然存​​在,您可能需要cmake按照@pdpcosta 的说明将其更新为更高版本,然后再次重复该过程。

回答by Lane Rettig

Even after adding -DPYTHON_INCLUDE_DIRand -DPYTHON_LIBRARYas suggested above, I was still facing the error Could NOT find PythonInterp. What solved it was adding -DPYTHON_EXECUTABLE:FILEPATH=to cmakeas suggested in https://github.com/pybind/pybind11/issues/99#issuecomment-182071479:

即使添加-DPYTHON_INCLUDE_DIR-DPYTHON_LIBRARY按照上面的建议,我仍然面临错误Could NOT find PythonInterp。什么解决它被添加-DPYTHON_EXECUTABLE:FILEPATH=cmake作为建议https://github.com/pybind/pybind11/issues/99#issuecomment-182071479

cmake .. \
-DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")  \
-DPYTHON_LIBRARY=$(python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
-DPYTHON_EXECUTABLE:FILEPATH=`which python`

回答by Guo Ang

Paste this into your CMakeLists.txt:

将其粘贴到您的 CMakeLists.txt 中:

# find python
execute_process(COMMAND python-config --prefix OUTPUT_VARIABLE PYTHON_SEARCH_PATH)
string(REGEX REPLACE "\n$" "" PYTHON_SEARCH_PATH "${PYTHON_SEARCH_PATH}")
file(GLOB_RECURSE PYTHON_DY_LIBS ${PYTHON_SEARCH_PATH}/lib/libpython*.dylib ${PYTHON_SEARCH_PATH}/lib/libpython*.so)
if (PYTHON_DY_LIBS)
    list(GET PYTHON_DY_LIBS 0 PYTHON_LIBRARY)
    message("-- Find shared libpython: ${PYTHON_LIBRARY}")
else()
    message(WARNING "Cannot find shared libpython, try find_package")
endif()

find_package(PythonInterp)
find_package(PythonLibs ${PYTHON_VERSION_STRING} EXACT)

回答by ZhaoGang

Note that if you are using cMake version 3.12 or later, variable PythonInterpand PythonLibshas been changed into Python.

请注意,如果您使用的是 cMake 3.12 或更高版本,则变量PythonInterpPythonLibs已更改为Python.

So we use:

所以我们使用:

find_package(Python ${PY_VERSION} REQUIRED)

find_package(Python ${PY_VERSION} REQUIRED)

instead of:

代替:

find_package(PythonInterp ${PY_VERSION} REQUIRED)find_package(PythonLibs ${PY_VERSION} REQUIRED)

find_package(PythonInterp ${PY_VERSION} REQUIRED)find_package(PythonLibs ${PY_VERSION} REQUIRED)

see https://cmake.org/cmake/help/v3.12/module/FindPython.htmlfor details.

有关详细信息,请参阅https://cmake.org/cmake/help/v3.12/module/FindPython.html

回答by jojo2000

In case that might help, I found a workaround for a similar problem, looking at the cmake doc : https://cmake.org/cmake/help/v3.0/module/FindPythonLibs.html

如果这可能有帮助,我找到了解决类似问题的方法,查看 cmake 文档:https: //cmake.org/cmake/help/v3.0/module/FindPythonLibs.html

You must set two env vars for cmake to find coherent versions. Unfortunately this is not a generic solution...

您必须为 cmake 设置两个环境变量才能找到一致的版本。不幸的是,这不是一个通用的解决方案......

cmake -DPYTHON_LIBRARY=${HOME}/.pyenv/versions/3.8.0/lib/libpython3.8.a -DPYTHON_INCLUDE_DIR=${HOME}/.pyenv/versions/3.8.0/include/python3.8/ cern_root/