Skip to content

Commit

Permalink
Improve python3 discovery code in CMake (pytorch#127600)
Browse files Browse the repository at this point in the history
The improvement is based on my comments in pytorch#124613 and it also fixes the current linux-s390x-binary-manywheel  CI failures.

Pull Request resolved: pytorch#127600
Approved by: https://github.com/Skylion007
  • Loading branch information
cyyever authored and pytorchmergebot committed May 31, 2024
1 parent 8d7393c commit 3e66052
Showing 1 changed file with 27 additions and 38 deletions.
65 changes: 27 additions & 38 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -834,53 +834,42 @@ else()
endif()
include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR})

# ---[ Python + Numpy
if(BUILD_PYTHON)
# If not given a Python installation, then use the current active Python
if(NOT Python_EXECUTABLE)
execute_process(
COMMAND "which" "python3" RESULT_VARIABLE _exitcode OUTPUT_VARIABLE _py_exe)
if(${_exitcode} EQUAL 0)
if(NOT MSVC)
string(STRIP ${_py_exe} Python_EXECUTABLE)
endif()
message(STATUS "Setting Python to ${Python_EXECUTABLE}")
endif()
endif()

# Check that Python works
set(PYTHON_VERSION)
if(DEFINED Python_EXECUTABLE)
execute_process(
COMMAND "${Python_EXECUTABLE}" "--version"
RESULT_VARIABLE _exitcode OUTPUT_VARIABLE PYTHON_VERSION)
if(NOT _exitcode EQUAL 0)
message(FATAL_ERROR "The Python executable ${Python_EXECUTABLE} cannot be run. Make sure that it is an absolute path.")
endif()
if(PYTHON_VERSION)
string(REGEX MATCH "([0-9]+)\\.([0-9]+)" PYTHON_VERSION ${PYTHON_VERSION})
# ---[ Python Interpreter
# If not given a Python installation, then use the current active Python
if(NOT Python_EXECUTABLE)
execute_process(
COMMAND "which" "python3" RESULT_VARIABLE _exitcode OUTPUT_VARIABLE _py_exe)
if(${_exitcode} EQUAL 0)
if(NOT MSVC)
string(STRIP ${_py_exe} Python_EXECUTABLE)
endif()
message(STATUS "Setting Python to ${Python_EXECUTABLE}")
endif()
endif()

# These should fill in the rest of the variables, like versions, but resepct
# the variables we set above
if(BUILD_PYTHON)
set(PYTHON_COMPONENTS Development)
if(USE_NUMPY)
find_package(Python COMPONENTS Interpreter Development NumPy)
else()
find_package(Python COMPONENTS Interpreter Development)
list(APPEND PYTHON_COMPONENTS NumPy)
endif()
find_package(Python COMPONENTS Interpreter OPTIONAL_COMPONENTS ${PYTHON_COMPONENTS})
else()
find_package(Python COMPONENTS Interpreter)
endif()

if(NOT Python_Development_FOUND)
message(FATAL_ERROR
"Python development libraries could not be found.")
endif()
if(NOT Python_Interpreter_FOUND)
message(FATAL_ERROR "Python3 could not be found.")
endif()

if(${Python_VERSION} VERSION_LESS 3.8)
message(FATAL_ERROR
"Found Python libraries version ${Python_VERSION}. Python < 3.8 is no longer supported by PyTorch.")
endif()
if(${Python_VERSION} VERSION_LESS 3.8)
message(FATAL_ERROR
"Found Python libraries version ${Python_VERSION}. Python < 3.8 is no longer supported by PyTorch.")
endif()

if(Python_Interpreter_FOUND)
# ---[ Python + Numpy
if(BUILD_PYTHON)
if(Python_Development_FOUND)
if(USE_NUMPY)
if(NOT Python_NumPy_FOUND)
message(WARNING "NumPy could not be found. Not building with NumPy. Suppress this warning with -DUSE_NUMPY=OFF")
Expand Down

0 comments on commit 3e66052

Please sign in to comment.