Skip to content

FindPython3 fails to find interpreter on Windows

This is a copy of my question on Stackoverflow. I originally posted there because I wasn't sure if this is a bug or I'm doing something wrong. (I'm new to CMake.) At this point, it looks like a bug to me.

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
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information