CMake在Windows上无法找到Python3解释器

4
这是我使用的CMakeLists.txt文件的开头:

CMakeLists.txt文件的开头如下:

cmake_minimum_required(VERSION 3.12)
project(hello-pyext)

find_package(Python3 COMPONENTS Interpreter Development)
message(STATUS
    "Python: version=${Python3_VERSION} interpreter=${Python3_EXECUTABLE}")
if(NOT Python3_FOUND AND Python3_Development_FOUND)
    #   find_package() will not abort the build if anything's missing.
    string(JOIN "\n" errmsg
        "  Python3 and/or development libs not found."
        "  - Python3_FOUND=${Python3_FOUND}"
        "  - Python3_Development_FOUND=${Python3_Development_FOUND}"
        )
    message(FATAL_ERROR ${errmsg})
endif()

"When built with cmake-3.12.4-Linux-x86_64 (downloaded from cmake.org) on Linux, it works fine, finding both the Python3 interpreter and development headers/libraries installed via apt-get. (Python2 is also installed on the system, but I've confirmed that the interpreter it finds is the Python 3 one.)

然而,在Windows 10上,它可以找到开发头文件和库,但无法找到解释器,输出:

"
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.14393.
-- Could NOT find Python3 (missing: Python3_EXECUTABLE Interpreter) (found version "3.6.6")
-- Python: version=3.6.6 interpreter=Python3_EXECUTABLE-NOTFOUND
CMake Error at hello-pyext/CMakeLists.txt:14 (message):
    Python3 and/or development libs not found.
    - Python3_FOUND=FALSE
    - Python3_Development_FOUND=TRUE

我在MinGW Bash和Visual Studio 2017的开发者命令提示符中使用了以下版本的CMake,结果相同:

  • 使用Visual Studio 2017安装程序自带的cmake version 3.12.18081601-MSVC_2
  • cmake.org下载的cmake-3.13.4-win64-x64
  • cmake.org下载的cmake-3.14.0-rc3-win64-x64
  • cmake.org下载的cmake-3.14.20190305-gc9ce4f-win64-x64(此编辑时最新的开发版本)

我只记得我曾经使用过从python.org下载的标准安装程序来安装Python。"程序和功能"列出了已安装的Python版本:Python 3.4.4 (64-bit)、Python 3.6.6 (64-bit)和Python Launcher。同时,py启动器可以正确启动这两个版本的Python,并且python也在我的环境变量中。

C:\>py --version
Python 3.6.6
C:\>py -3.4 --version
Python 3.4.4
C:\>python --version
Python 3.6.6
C:\>python3 --version
'python3' is not recognized as an internal or external command,
operable program or batch file.
C:\>

我还与另一位开发人员的机器进行了对比,他将Python 3.5安装为Anaconda的主要Python,并从python.org上进行了3.6安装,结果也相同。
已弃用的“FindPythonInterp”似乎可以正常工作:
find_package(PythonInterp)
message("
    PYTHONINTERP_FOUND=${PYTHONINTERP_FOUND}
    PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}
    PYTHON_VERSION_STRING=${PYTHON_VERSION_STRING}
")

-- Found PythonInterp: C:/Program Files/Python36/python.exe (found version "3.6.6")

    PYTHONINTERP_FOUND=TRUE
    PYTHON_EXECUTABLE=C:/Program Files/Python36/python.exe
    PYTHON_VERSION_STRING=3.6.6

我对Windows不太熟悉,所以我不确定该从哪里开始调试。有没有人知道为什么FindPython3无法找到解释器,或者我应该如何开始调试(除了阅读FindPython3的源代码)?

1个回答

7
根据[CMake问题19024(https://gitlab.kitware.com/cmake/cmake/issues/19024)],问题在于我正在32位系统上进行构建(默认值,因为我没有使用-A x64配置),但该系统只安装了64位Python。 FindPython3感觉到它已经找到了32位的开发工具(实际上并没有),意识到它找不到32位解释器,因此设置 Python3_FOUND=False
通过使用-A x64配置进行64位构建可以解决这个问题。
“找到不在那里的32位开发工具”问题(导致在上述问题中打印出Python3_Development_FOUND=TRUE)是FindPython3模块中的一个错误,已经通过MR 3103得到修复,并在20190316夜间构建中提供。不幸的是,这并没有被包含在3.14.0版本中。
作为参考,在FindPython3成功工作后构建扩展所需执行以下操作:
Python3_add_library(myext MODULE myextsrc)
target_link_libraries(myext other_target_on_which_it_depends)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接