@@ -138,14 +138,21 @@ foreach(link ${content_links})
138138endforeach ()
139139ExternalData_Add_Target(ITKSphinxExamplesData)
140140
141- if (${ITK_WRAP_PYTHON} )
142- # Use the same Python interpreter as ITK to run the tests.
143- find_package (PythonInterp ${ITK_WRAP_PYTHON_VERSION} REQUIRED)
144- mark_as_advanced (PYTHON_EXECUTABLE)
141+ set (PYTHON_REQUIRED_VERSION 3.6)
142+ set (Python_ADDITIONAL_VERSIONS 3.12 3.11 3.10 3.9 3.8 3.7 3.6)
143+ if (BUILD_TESTING OR ITK_BUILD_DOCUMENTATION OR ITK_WRAP_PYTHON)
144+ if (ITK_WRAP_PYTHON)
145+ # Use the same Python interpreter as ITK to run the tests.
146+ find_package (Python3 ${ITK_WRAP_PYTHON_VERSION} EXACT COMPONENTS Interpreter REQUIRED)
147+ mark_as_advanced (Python3_EXECUTABLE)
148+ else ()
149+ find_package (Python3 ${PYTHON_REQUIRED_VERSION} COMPONENTS Interpreter)
150+ endif ()
145151endif ()
146- find_package (PythonInterp)
147- if (PYTHON_EXECUTABLE AND NOT ITK_WRAP_PYTHON)
148- execute_process (COMMAND ${PYTHON_EXECUTABLE} -c "import itk"
152+
153+ if (Python3_EXECUTABLE AND NOT ITK_WRAP_PYTHON)
154+ # See if external ITK python packages are installed for this python environment
155+ execute_process (COMMAND ${Python3_EXECUTABLE} -c "import itk"
149156 RESULT_VARIABLE _have_itk_return_code
150157 OUTPUT_QUIET
151158 ERROR_QUIET
@@ -154,62 +161,6 @@ if(PYTHON_EXECUTABLE AND NOT ITK_WRAP_PYTHON)
154161 set (ITK_WRAP_PYTHON 1)
155162 endif ()
156163endif ()
157- if (BUILD_TESTING OR ITK_BUILD_DOCUMENTATION OR ITK_WRAP_PYTHON)
158- # Prefer to use more robust FindPython3 module if greater than cmake 3.12.0
159- if ("${CMAKE_VERSION} " VERSION_LESS_EQUAL "3.12.0" )
160- # Use of PythonInterp and PythonLibs is deprecated since cmake version 3.12.0
161- # Only use deprecated mechanisms for older versions of cmake
162- set (Python_ADDITIONAL_VERSIONS 3.9 3.8 3.7 3.6 3.5)
163- find_package (PythonInterp)
164- if (ITK_WRAP_PYTHON)
165- find_package (PythonLibs REQUIRED)
166- # check for version mismatch.
167- if (PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND
168- AND NOT (PYTHON_VERSION_STRING VERSION_EQUAL PYTHONLIBS_VERSION_STRING))
169- message (FATAL_ERROR "Python executable (\" ${PYTHON_VERSION_STRING} \" ) and library (\" ${PYTHONLIBS_VERSION_STRING} \" ) version mismatch." )
170- endif ()
171- endif ()
172- if (PYTHON_VERSION_STRING VERSION_LESS 3.5)
173- # if python version is less than 3.5, unset so that it appears that no python version is found.
174- # to emulate the same behavior as find(Python3 ..) from cmake 3.12.0+
175- unset (PYTHON_EXECUTABLE)
176- unset (PYTHONINTERP_FOUND)
177- unset (PYTHON_VERSION_STRING)
178- unset (PYTHON_INCLUDE_DIRS)
179- else ()
180- ## For forward compatibility with cmake 3.12.0 or greater, emulate variable names from FindPython3.cmake
181- set (Python3_EXECUTABLE ${PYTHON_EXECUTABLE} )
182- set (Python3_Interpreter_FOUND ${PYTHONINTERP_FOUND} )
183- set (Python3_VERSION ${PYTHON_VERSION_STRING} )
184-
185- set (Python3_Development_FOUND ${PYTHONLIBS_FOUND} )
186- set (Python3_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} )
187- set (Python3_LIBRARIES ${PYTHON_LIBRARIES} )
188- endif ()
189- else ()
190- if (ITK_WRAP_PYTHON)
191- if (DEFINED Python3_EXECUTABLE)
192- set (_specified_Python3_EXECUTABLE ${Python3_EXECUTABLE} )
193- endif ()
194- find_package (Python3 COMPONENTS Interpreter Development)
195- set (Python3_EXECUTABLE ${_specified_Python3_EXECUTABLE} CACHE INTERNAL
196- "Path to the Python interpreter" FORCE)
197- else ()
198- find_package (Python3 COMPONENTS Interpreter)
199- endif ()
200- if (NOT Python3_EXECUTABLE AND _Python3_EXECUTABLE)
201- set (Python3_EXECUTABLE ${_Python3_EXECUTABLE} CACHE INTERNAL
202- "Path to the Python interpreter" FORCE)
203- endif ()
204- endif ()
205-
206- if (ITK_WRAP_PYTHON AND Python3_VERSION VERSION_LESS 3.5)
207- message (FATAL_ERROR "Python versions less than 3.5 are not supported for wrapping. Python version: \" ${Python3_VERSION} \" ." )
208- endif ()
209- if (ITK_WRAP_PYTHON AND NOT Python3_INCLUDE_DIRS)
210- message (FATAL_ERROR "Python version ${Python3_VERSION} development environment not found for wrapping." )
211- endif ()
212- endif ()
213164
214165# Build the documentation?
215166option (BUILD_DOCUMENTATION "Build the examples documentation." OFF )
@@ -265,7 +216,6 @@ if(BUILD_DOCUMENTATION)
265216 endif ()
266217
267218 # Builds the documentation.
268- find_package (PythonInterp REQUIRED)
269219 find_package (Sphinx REQUIRED)
270220 find_program (PANDOC_BIN pandoc REQUIRED)
271221
@@ -288,7 +238,7 @@ if(BUILD_DOCUMENTATION)
288238 )
289239 add_custom_command (TARGET ${sphinx_target_base_name} _html
290240 POST_BUILD
291- COMMAND ${PYTHON_EXECUTABLE }
241+ COMMAND ${Python3_EXECUTABLE }
292242 ${ITKSphinxExamples_SOURCE_DIR} /Utilities/DocumentationTarball.py
293243 ${SPHINX_DESTINATION} /html
294244 ${ITKSphinxExamples_BINARY_DIR} /ITKSphinxExamples-${ITKSphinxExamples_RELEASE_VERSION} -html
0 commit comments