CMake fails to find system python2 when using a python3 virtual environment
Hi!
I'm working on a C/C++ project that also needs python2/3. We recently moved to using python virtual environments and I've noticed that CMake has issues finding the system version of python2. This is on Ubuntu 16/18.
Since you can't have both python 2 and 3 on a virtual environment, we are using a python 3 virtual env and for the legacy python2 code we want to use the system version.
I have the following CMake flags set:
set(Python_FIND_VIRTUALENV ONLY)
set(Python2_FIND_VIRTUALENV STANDARD)
Which produces the following errror when it encounters a find_package(Python2 REQUIRED)
:
CMake Error at .poetry/virtualenvs/project-ixRgv7Gw-py3.6/lib/python3.6/site-packages/cmake/data/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
Could NOT find Python2 (missing: Python2_EXECUTABLE Interpreter) (found
version "2.7.17")
Reason given by package:
Interpreter: Wrong major version for the interpreter "/home/eric/project/.poetry/virtualenvs/project-ixRgv7Gw-py3.6/bin/python"
Call Stack (most recent call first):
.poetry/virtualenvs/project-ixRgv7Gw-py3.6/lib/python3.6/site-packages/cmake/data/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:458 (_FPHSA_FAILURE_MESSAGE)
.poetry/virtualenvs/project-ixRgv7Gw-py3.6/lib/python3.6/site-packages/cmake/data/share/cmake-3.18/Modules/FindPython/Support.cmake:2966 (find_package_handle_standard_args)
.poetry/virtualenvs/project-ixRgv7Gw-py3.6/lib/python3.6/site-packages/cmake/data/share/cmake-3.18/Modules/FindPython2.cmake:336 (include)
aristotle/CMakeLists.txt:53 (find_package)
So it somehow picks up the python3 executable from the virtual env and then complains that it is the wrong version. It looks to be related to the virtual environment creating python -> python3
symlink.
I also noticed that adding this flag fixes the issue, but I'm not sure why:
set(Python2_FIND_STRATEGY VERSION)
Is this a bug or am I doing something wrong?
Is there a way to set Python2_FIND_VIRTUALENV
to NEVER
so it never looks for it inside the virtual environment?
Thanks!