【问题标题】:CMake FindPython3 failing to find interpreter on WindowsCMake FindPython3 无法在 Windows 上找到解释器
【发布时间】:2019-03-06 11:27:03
【问题描述】:

这是我正在使用的 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()

在 Linux 上使用 cmake-3.12.4-Linux-x86_64(从 cmake.org 下载)构建时,它工作正常,可以找到通过 apt-get 安装的 Python3 解释器和开发头文件/库。 (系统上也安装了 Python2,但我已经确认它找到的解释器是 Python 3。)

然而,在 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

我在 VS 2017 的 MinGW Bash 和 Developer Command Prompt 中得到了相同的结果,以及以下所有版本的 CMake:

  • cmake version 3.12.18081601-MSVC_2 随 Visual Studio 2017 提供。
  • cmake-3.13.4-win64-x64cmake.org下载。
  • cmake-3.14.0-rc3-win64-x64cmake.org下载。
  • cmake-3.14.20190305-gc9ce4f-win64-x64(本次编辑的最新开发版本)从 cmake.org 下载

据我所知,我只使用过来自python.org 的标准安装程序来安装 Python。 “程序和功能”列出了已安装的 Python 3.4.4(64 位)、Python 3.6.6(64 位)和 Python Launcher。 py 启动器正确启动这两个,以及 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:\>

我还检查了一位开发人员的机器,他通过 Anaconda 安装了 Python 3.5 作为他的主要 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 的源代码)?

【问题讨论】:

    标签: python windows cmake visual-studio-2017


    【解决方案1】:

    这里的问题,根据 [CMake issue 19024(https://gitlab.kitware.com/cmake/cmake/issues/19024),是我在一个只有 64安装了位 Python。 FindPython3 觉得它找到了 32 位开发工具(虽然它没有),意识到它找不到 32 位解释器,所以设置了Python3_FOUND=False

    通过配置-A x64 进行 64 位构建修复了该问题。

    “查找不存在的 32 位开发工具”问题(导致它在上面的问题中打印 Python3_Development_FOUND=TRUE)是 FindPython3 模块中的一个错误,已由 MR 3103 修复,可用在 20190316 nightly build 中。不幸的是,这并没有进入 3.14.0 版本。

    作为参考,您想要在 FindPython3 成功运行后构建您的扩展是:

    Python3_add_library(myext MODULE myextsrc)
    target_link_libraries(myext other_target_on_which_it_depends)
    

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2017-09-25
      • 2021-01-16
      相关资源
      最近更新 更多