Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake find_package pythoninterp is deprecated #1112

Conversation

VincentRouvreau
Copy link
Contributor

Replaces #1007
Modification taken from #151
Fix #139
CMake 3.12 is now 5 years old 3.15 is now 4 years old (july 2019), so it should be ok for every one.
CMake 3.15 is required for cmake_policy(SET CMP0094 NEW)
CMake 3.16 (nov 2019) is required if the user wants to specify a python interpreter (cmake -DPython_EXECUTABLE=/custom/path/to/python3 ...)

# Can be set with -DPython_EXECUTABLE=/usr/bin/python3 for instance.
# CMP0094 OLD finds system python interpreter instead of conda even when using '-DCMAKE_PREFIX_PATH=$CONDA_PREFIX'
cmake_policy(SET CMP0094 NEW)
find_package( Python COMPONENTS Interpreter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not the following, to make it less likely to pick a bad version of Python?

Suggested change
find_package( Python COMPONENTS Interpreter)
find_package( Python COMPONENTS Interpreter Development.Module NumPy)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see a lot of added value with Development.Module or NumPy. In the documentation, you can get Python_NumPy_INCLUDE_DIRS, that I get in setup.py.in with the python code from numpy import get_include as numpy_get_include, and Python_NumPy_VERSION, that I get with the CMake function find_python_module (that performs ${Python_EXECUTABLE} -c "import ${PYTHON_MODULE_NAME}; print(${PYTHON_MODULE_NAME}.__version__), and by the way I also need this function for the other python dependencies to decide if a test must be passed or not. Maybe we could also skip tests when import throw ImportError.

I am not sure this would help to understand this ugly CMakeLists.txt... But tell me if you disagree, I can try

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see a lot of added value with Development.Module or NumPy.

I didn't suggest them for the variables they define, but because if they are present, find_package is supposed to find a version of Python that has the dev stuff and numpy installed. If there are several versions of Python installed, this makes it less likely to pick one that is unusable for our purpose. (we do need the dev stuff to compile a plugin)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that makes sense. I added your proposal on 23a393b

@VincentRouvreau VincentRouvreau marked this pull request as ready for review August 6, 2024 15:13
IF %ERRORLEVEL% NEQ 0 EXIT 1
ctest --output-on-failure -C Release -E diff_files
IF %ERRORLEVEL% NEQ 0 EXIT 1
for /f %%i in ('where python') do set PYTHON_EXE=%%i
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, this sets PYTHON_EXE to the last entry returned by where python? That's a bit strange.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it seems to be the good way to do it in batch 😅 cf. https://devblogs.microsoft.com/oldnewthing/20120731-00/?p=7003

If you prefer, I can do:

          for /f %%i in ('where python') do (set PYTHON_EXE=%%i & goto :next)
          :next

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my question is not so much the syntax (this is CI, I don't much care), it is more trying to understand why we need to specify python now when we didn't before. What versions of Python exist under what names on that machine, which one do we want (apparently the only one called plain python?), and which one does CMake choose (and ideally why). Again, I don't really care about the CI case in particular, I care more that this is a fallout of the cmake change which could also affect users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, and I did not talk too much about my experiments around this new feature, my fault too.
If you have a look to https://github.com/VincentRouvreau/play_with_azure_pipelines/blob/159a010d99352edd169949c700af23940b5eabe0/azure-pipelines.yml#L44-L58

In a CMakeLists.txt, I just call find_package( Python COMPONENTS Interpreter).

  1. I first call cmake without any arguments, I get on Azure CI (you may not have access to the logs):
    -- Found Python: C:/hostedtoolcache/windows/Python/3.12.4/x64/python3.exe (found version "3.12.4") found components: Interpreter
    Note this is not correct, because I said in the yaml file:
    steps:
      - task: UsePythonVersion@0
        displayName: Use Python $(pythonVersion)
        inputs:
          versionSpec: 3.9
          addToPath: true
          architecture: "x64"
  1. Then I call:
for /f %%i in ('where python') do echo %%i
# Prints: C:\hostedtoolcache\windows\Python\3.9.13\x64\python.exe
for /f %%i in ('where python') do (set PYTHON_EXE=%%i & goto :next)
:next
cmake -DPython_EXECUTABLE=%PYTHON_EXE% ..
# -- Found Python: C:\hostedtoolcache\windows\Python\3.9.13\x64\python.exe (found version "3.9.13") found components: Interpreter

I don't know how it will affect the users, but for sure it won't affect main users that are only using conda or pip packages.
I also don't know how the python installation is performed on the Azure Windows virtual machines, but the behaviour is quite strange... I can have a look if there is something that could replace UsePythonVersion or if its installation is not well performed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the system has other versions of Python installed in addition to the one we request, that's fine. The one we request is the only one in the default search path, which is good. However, Python_FIND_REGISTRY is FIRST by default, meaning "Try to use registry before environment variables.", and the registry probably knows both versions of Python and not our preferences. It looks like we could pass -DPython_FIND_REGISTRY=LAST (or NEVER) to cmake in this CI. Does that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I got it and tested it on https://github.com/VincentRouvreau/play_with_azure_pipelines it works !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used -DPython_FIND_REGISTRY=LAST and -DPython_FIND_FRAMEWORK=LAST on d190e7d

src/python/CMakeLists.txt Outdated Show resolved Hide resolved
VincentRouvreau and others added 3 commits August 27, 2024 15:02
Co-authored-by: Marc Glisse <marc.glisse@inria.fr>
…centRouvreau/gudhi-devel into find_package_pythoninterp_deprecated
# Can be set with -DPython_EXECUTABLE=/usr/bin/python3 for instance.
# CMP0094 OLD finds system python interpreter instead of conda even when using '-DCMAKE_PREFIX_PATH=$CONDA_PREFIX'
cmake_policy(SET CMP0094 NEW)
find_package( Python COMPONENTS Interpreter Development.Module NumPy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Development.Module (as opposed to plain Development which also includes Development.Embed) requires CMake 3.18, so we should either require that, or say Development (and put a comment next to it to remind us to update it in a couple years), or remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find_package( Python COMPONENTS Interpreter Development NumPy) and a comment on 630094b

IF %ERRORLEVEL% NEQ 0 EXIT 1
ctest --output-on-failure -C Release -E diff_files
IF %ERRORLEVEL% NEQ 0 EXIT 1
for /f %%i in ('where python') do set PYTHON_EXE=%%i
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the system has other versions of Python installed in addition to the one we request, that's fine. The one we request is the only one in the default search path, which is good. However, Python_FIND_REGISTRY is FIRST by default, meaning "Try to use registry before environment variables.", and the registry probably knows both versions of Python and not our preferences. It looks like we could pass -DPython_FIND_REGISTRY=LAST (or NEVER) to cmake in this CI. Does that work?

@@ -8,6 +8,10 @@ jobs:
variables:
pythonVersion: "3.9"
cmakeBuildType: Release
gudhiCmakeOptions: -DWITH_GUDHI_EXAMPLE=ON -DWITH_GUDHI_TEST=ON -DWITH_GUDHI_UTILITIES=ON -DWITH_GUDHI_PYTHON=ON -DWITH_GUDHI_REMOTE_TEST=ON
# On this VM, 2 versions of python are installed. Default Python_FIND_FRAMEWORK is FIRST, which means asks frameworks first
# LAST means consult frameworks in last resort, use the standard libraries or headers first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect it uses PATH, not "standard libraries or headers", since it is looking for a python exe?

@VincentRouvreau VincentRouvreau added the build The build system (CMake, etc) label Aug 30, 2024
@VincentRouvreau VincentRouvreau merged commit 2a89fd5 into GUDHI:master Aug 30, 2024
4 of 7 checks passed
@VincentRouvreau VincentRouvreau deleted the find_package_pythoninterp_deprecated branch August 30, 2024 09:21
@VincentRouvreau VincentRouvreau added the 3.11.0 GUDHI version 3.11.0 label Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11.0 GUDHI version 3.11.0 build The build system (CMake, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FindPythonInterp deprecated
2 participants