Skip to content

Commit

Permalink
When finding Python3, use python3 executable as a hint (#650)
Browse files Browse the repository at this point in the history
The existing find_package(Python3) call will locate the Python
interpreter with the latest version number. However, this may not be the
system's default Python interpreter for which our dependencies have been
installed.

If Python3_EXECUTABLE is not explicitly specified, use find_program to
locate the Python interpreter behind the "python3" command, which is
likely the system's default and the one that we want.

If no executable can be found by the name "python3", the find_program
call will silently fail and the existing behavior will manifest.

Signed-off-by: Scott K Logan <logans@cottsay.net>
  • Loading branch information
cottsay committed Feb 16, 2024
1 parent bda8c49 commit a3db20f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tf2_geometry_msgs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
find_package(PythonExtra REQUIRED)
# Force FindPython3 to use the debug interpretter where ROS 2 expects it
set(Python3_EXECUTABLE "${PYTHON_EXECUTABLE_DEBUG}")
elseif(NOT WIN32 AND NOT Python3_EXECUTABLE)
# We expect that Python dependencies are met for whatever Python interpreter
# is invoked by the "python3" executable, however this may not be the latest
# Python version installed on the system. The default behavior of
# find_package(Python3) is to use the latest version (e.x. python3.12), so we
# specifically look for a "python3" executable and if found, instruct
# find_package(Python3) to use that.
# On Windows, the find_package(Python3) logic is different and doesn't
# appear to prefer specific versions (e.x. python3.12) over plain
# "python.exe" so this extra logic is unnecessary there.
find_program(Python3_EXECUTABLE python3)
endif()
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)

Expand Down
11 changes: 11 additions & 0 deletions tf2_py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
find_package(PythonExtra REQUIRED)
# Force FindPython3 to use the debug interpretter where ROS 2 expects it
set(Python3_EXECUTABLE "${PYTHON_EXECUTABLE_DEBUG}")
elseif(NOT WIN32 AND NOT Python3_EXECUTABLE)
# We expect that Python dependencies are met for whatever Python interpreter
# is invoked by the "python3" executable, however this may not be the latest
# Python version installed on the system. The default behavior of
# find_package(Python3) is to use the latest version (e.x. python3.12), so we
# specifically look for a "python3" executable and if found, instruct
# find_package(Python3) to use that.
# On Windows, the find_package(Python3) logic is different and doesn't
# appear to prefer specific versions (e.x. python3.12) over plain
# "python.exe" so this extra logic is unnecessary there.
find_program(Python3_EXECUTABLE python3)
endif()
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)

Expand Down

0 comments on commit a3db20f

Please sign in to comment.