My Centos7 has python2.7 in /usr/local/bin.
I installed python3.6.8 in /bin
The PATH variable has “/usr/local/bin:/bin”, so /usr/local/bin is ahead of /bin.
The following command in my script fails:
#Python3.6 or later is needed:
include(FindPython3)
if(NOT Python3_Interpreter_FOUND)
message(FATAL_ERROR "Python3 is needed, please install it first")
endif()
The error message is:
-- Could NOT find Python3 (missing: Python3_EXECUTABLE Interpreter)
Reason given by package:
Interpreter: Wrong major version for the interpreter "/usr/local/bin/python"
CMake Error at CMakeLists.txt:59 (message):
Python3 is needed, please install it first
It looks like FindPython3 stops at /usr/local/bin, and not searched /bin at all.
How to fix this? Is there a robust way for FindPython3 to work?
I don’t think it knows to search for patch versions on the binary name. Is it /bin/python3.6.8 or is it /bin/python3.6? If the latter, could you post the (relevant) output of cmake --debug-find to help see what CMake is searching for.
First, the usage of FindPython3 is wrong. To have a correct behavior, please use find_package() command rather than include.
find_package(Python3)
Second, in your case, to ensure correct behavior, you have to ignore unversioned names (i.e. python) because python 2 is located in a path beforepython 3:
That works if the Python you want to find is the newest on the machine. If, e.g., you’re trying to build against a 3.6 but 3.9 is on the system, it’s going to ignore your 3.6. I think it’s best to use the LOCATION strategy in general and set the executable or a prefix/root variable, but that’s me.