Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake FindPython3 failing to find interpreter on Windows

Here's the start of a CMakeLists.txt file that I'm using:

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

On Windows 10, however, it finds the development headers/libs but not the interpreter, printing:

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

I get the same results in both MinGW Bash and Developer Command Prompt for VS 2017, with all of the following versions of CMake:

  • cmake version 3.12.18081601-MSVC_2 that came with Visual Studio 2017.
  • cmake-3.13.4-win64-x64 downloaded from cmake.org.
  • cmake-3.14.0-rc3-win64-x64 downloaded from cmake.org.
  • cmake-3.14.20190305-gc9ce4f-win64-x64 (latest development version as of this edit) downloaded from cmake.org

As best I recall, I've only ever used the standard installers from python.org to install Python. "Programs and Features" lists Python 3.4.4 (64-bit), Python 3.6.6 (64-bit) and Python Launcher as being installed. The py launcher correctly launches both of these, as well as python itself being in my path:

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:\>

I've also checked this against a fellow developer's machine who installed Python 3.5 via Anaconda as his primary Python, as well as a 3.6 install from python.org, and gotten the same results.

The deprecated FindPythonInterp does seem to work:

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

I'm not too familiar with Windows, so I'm not sure where to go from here to debug this. Does anybody have any idea why FindPython3 is unable to find the interpreter, or how I should start debugging this (beyond just reading the source code for FindPython3)?

like image 552
cjs Avatar asked Oct 18 '25 06:10

cjs


1 Answers

The problem here, as per [CMake issue 19024(https://gitlab.kitware.com/cmake/cmake/issues/19024), is that I was doing a 32-bit build (the default, since I'd not configured with -A x64) on a system with only 64-bit Python installed. FindPython3 felt it had found the 32-bit development tools (though it hadn't), realized that it couldn't find a 32-bit interpreter and so set Python3_FOUND=False.

Doing a 64-bit build by configuring with -A x64 fixed that problem.

The "finding 32-bit dev tools that aren't there" problem (causing it to print Python3_Development_FOUND=TRUE in the question above) was a bug in the FindPython3 module which has been fixed by MR 3103, available in the 20190316 nightly build. Unfortunately this did not make it into the 3.14.0 release.

For reference, what you want to do to build your extension after FindPython3 as worked successfully is:

Python3_add_library(myext MODULE myextsrc)
target_link_libraries(myext other_target_on_which_it_depends)
like image 200
cjs Avatar answered Oct 20 '25 19:10

cjs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!