Cmake无法找到Python库

时间:2014-06-11 23:48:34

标签: python python-2.7 cmake

出现此错误:

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

我已安装:

  1. sudo apt-get install python-dev
  2. 环境变量已设置如下:

    PYTHON_INCLUDE_DIRS=/usr/include/python2.7 
    PYTHON_LIBRARIES=/usr/lib/python2.7/config/libpython2.7.so
    
  3. python.h的位置:/usr/lib/include/python2.7/python.h

    python个库的位置:/usr/lib/python2.7/ 怎么解决这个?

11 个答案:

答案 0 :(得分:26)

您可以通过在cmake命令后附加填充相应文件夹的-DPYTHON_LIBRARY-DPYTHON_INCLUDE_DIR标记来修复错误。

因此,诀窍是使用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'))")

如果你要链接到cmake的python版本是Python3.X而默认的python符号链接指向Python2.X,则可以使用python3 -c ...代替python -c ...

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

答案 1 :(得分:15)

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

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

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

答案 2 :(得分:14)

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:

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.

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

答案 3 :(得分:9)

对我来说下一步是有帮助的:

> apt-get install python-dev python3-dev

答案 4 :(得分:7)

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

尝试在set(Python_ADDITIONAL_VERSIONS 3.4)声明之前添加find_package

请记得清除CMakeCache.txt

答案 5 :(得分:7)

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

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

只需创建.reg文件即可设置必要的密钥并进行相应编辑以符合您的设置。

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Python]

[HKEY_CURRENT_USER\Software\Python\Pythoncore]

[HKEY_CURRENT_USER\Software\Python\Pythoncore\2.6]

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

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

[HKEY_CURRENT_USER\Software\Python\Pythoncore\2.7]

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

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

答案 6 :(得分:3)

即使按照上面的建议添加-DPYTHON_INCLUDE_DIR-DPYTHON_LIBRARY,我仍然面临错误Could NOT find PythonInterp。解决这个问题的方法是按照https://github.com/pybind/pybind11/issues/99#issuecomment-182071479

中的建议将-DPYTHON_EXECUTABLE:FILEPATH=添加到cmake
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`

答案 7 :(得分:0)

将此粘贴​​到您的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)

答案 8 :(得分:0)

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

因此我们使用:

find_package(Python ${PY_VERSION} REQUIRED)

代替:

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

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

答案 9 :(得分:0)

以防万一,我通过查看cmake doc找到了类似问题的解决方法: https://cmake.org/cmake/help/v3.0/module/FindPythonLibs.html

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

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/

答案 10 :(得分:0)

如果以上所有方法都不起作用,也许下面的命令行对您有用。

sudo apt-get install cython cython3