Cur*_*son 4 python windows cmake visual-studio-2017
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()
Run Code Online (Sandbox Code Playgroud)
当在 Linux 上使用cmake-3.12.4-Linux-x86_64(从 下载cmake.org)构建时,它工作正常,可以找到通过apt-get. (系统上也安装了Python2,但我已经确认它找到的解释器是Python 3。)
然而,在 Windows 10 上,它找到开发标头/库,但找不到解释器,并打印:
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()
Run Code Online (Sandbox Code Playgroud)
我在 MinGW Bash 和 VS 2017 的开发人员命令提示符中得到相同的结果,并且使用以下所有版本的 CMake:
cmake version 3.12.18081601-MSVC_2Visual Studio 2017 附带的。cmake-3.13.4-win64-x64下载自cmake.org.cmake-3.14.0-rc3-win64-x64下载自cmake.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它本身位于我的路径中:
-- 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
Run Code Online (Sandbox Code Playgroud)
我还针对一位开发人员同事的机器进行了检查,该机器通过 Anaconda 安装了 Python 3.5 作为他的主要 Python,并从 中安装了 3.6 python.org,并得到了相同的结果。
已弃用的FindPythonInterp似乎确实有效:
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:\>
Run Code Online (Sandbox Code Playgroud)
find_package(PythonInterp)
message("
PYTHONINTERP_FOUND=${PYTHONINTERP_FOUND}
PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}
PYTHON_VERSION_STRING=${PYTHON_VERSION_STRING}
")
Run Code Online (Sandbox Code Playgroud)
我对 Windows 不太熟悉,所以我不知道从这里到哪里去调试它。有谁知道为什么FindPython3无法找到解释器,或者我应该如何开始调试它(除了阅读 的源代码之外FindPython3)?
这里的问题,根据[CMake问题19024(https://gitlab.kitware.com/cmake/cmake/issues/19024),是我正在做一个32位构建(默认值,因为我没有配置)-A x64在仅安装了 64 位 Python 的系统上。FindPython3 感觉它已经找到了 32 位开发工具(尽管它没有),意识到它找不到 32 位解释器,因此设置了Python3_FOUND=False.
通过配置进行 64 位构建-A x64修复了该问题。
“查找不存在的 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)
Run Code Online (Sandbox Code Playgroud)