<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/126162>126162</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Inconsistency when searching for Python3 and Python
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dbabokin
</td>
</tr>
</table>
<pre>
I've just stepped into a trap caused by these lines of CMake code:
https://github.com/llvm/llvm-project/blob/b7279ed5b3ae3e7b0fd61e0f08c86fb1958f0b6f/mlir/cmake/modules/MLIRDetectPythonEnv.cmake#L25-L28
I was building [torch-mlir](https://github.com/llvm/torch-mlir) on my Macbook and I was following the official build guidelines. On my system I have Python3.13 and Python3.11, but I was running in VirtualEnv with Python3.11 enabled. And CMake behaved really weirdly. It looked for Python three times, every time it was looking for `Python3` twice and once for `Python` (that's expected). While it consistently found `Python3` with version `3.11`, the `Python` was found as `3.11` at first, but then as `3.13` (really unexpected).
Then if I was rerunning `cmake` with exactly the same command line (which succeeded on the first pass), it crashed with the following message:
```
CMake Error at /Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:233 (message):
Could NOT find Python (missing: Development.Module) (found suitable version
"3.11.11", minimum required is "3.8")
Call Stack (most recent call first):
/Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:603 (_FPHSA_FAILURE_MESSAGE)
/Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPython/Support.cmake:4002 (find_package_handle_standard_args)
/Users/dbabokin/ox-workspaces/torch-mlir/.venv_torch_mlir2/lib/python3.11/site-packages/cmake/data/share/cmake-3.31/Modules/FindPython.cmake:631 (include)
/Users/dbabokin/ox-workspaces/torch-mlir/externals/llvm-project/mlir/cmake/modules/MLIRDetectPythonEnv.cmake:27 (find_package)
/Users/dbabokin/ox-workspaces/torch-mlir/externals/llvm-project/mlir/CMakeLists.txt:196 (mlir_configure_python_dev_packages)
```
Running `cmake` again was successful, one more time - failing.
Debugging the problem I found out that the root cause was that I passed `-DPython3_FIND_VIRTUALENV=ONLY` to CMake, but not `-DPython_FIND_VIRTUALENV=ONLY`!!!
So that resolved my problem and in this sense it was a user error. But I think it really makes sense to check that `Python3_FIND_VIRTUALENV` and `Python_FIND_VIRTUALENV` are defined (or not defined) in the same way and warn user otherwise.
Tagging @makslevental who is the last one who touched `find_package(Python ...)` code.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUV92O27gVfhr65sCCTI1l-cIXTjzqGphJgpkkRa8MijySuKZIl6Ts8dsXpCSPk92i6AIFGmAAj6jz850_fkfMOdloxA1ZfiDL3Yz1vjV2IypWmaPUs8qI62ZP6OqM8HvvPDiPpxMKkNobYOAtOwFnvUMB1RV8iw5BSY0OTA0fn9kRgRuBJNuSdNt6f3LhX1oSWjbSt32VcNMRWip1nn7mJ2t-R-4JLStlqvCzoqs1imWVMcxwVaW1yBeY1mnBi7yuFutlUadVXhNadkpaQkvesSOGRyN6hY7Q8vlp_7JDj9x_ufrW6Ed9Tkap7Iku50-0IGkAuYcLc1D1UgmpGyDLD95Y3s6j5eWO0OI_h3GnQddgNHRXeGa8MuYITAsYfNRGKXMJTnyLYOpacsnU4BqaXgqMmUzgczTgrs5jB3to2RlhiCJLFlm0eHtcEPoRqt6PPmyvdfAgNXyX1vdMPeozXKRv71QANasUigS2WoxVqzC4EWCRKXWFC0or1DWBvQdlzBEF1MaONsC3FhG87EKqPwKe0V7jI0gfYQSVACPokDwdXZM8BX-RHGMIRnP8USC8J7TwLfOErhzg2wm5R0HoOoG_t1JF-9xoJ51H7dUVatNr8ZOLGO0ZrZNGh1cxS3kakIb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