hunter_venvΒΆ

This package is used to create an isolated Python environment inside Hunter and is a workaround for missing Python package. It is designed to be used with FindPython module. CMake 3.13 is a minimum required (see details below).

hunter_add_package(hunter_venv)
find_package(hunter_venv CONFIG REQUIRED)

find_package(Python REQUIRED)

add_custom_target(python_version ALL Python::Interpreter --version)

execute_process(COMMAND ${Python_EXECUTABLE} --version RESULT_VARIABLE result)
if(NOT result EQUAL "0")
  message(FATAL_ERROR "Failed")
endif()

Python version that will be used to create environment can be set by HUNTER_VENV_PYTHON_VERSION variable:

# local config.cmake

hunter_config(
    hunter_venv
    VERSION ${HUNTER_hunter_venv_VERSION}
    CMAKE_ARGS HUNTER_VENV_PYTHON_VERSION=3.6.7
)

Requested Python version and virtualenv should be installed in a system.

Default values for HUNTER_VENV_PYTHON_VERSION will match testing CI environment of Travis/AppVeyor machines:

if(APPLE)
  set(__hunter_venv_default_python "3.6.5")
elseif(WIN32)
  set(__hunter_venv_default_python "3.6.8")
else()
  set(__hunter_venv_default_python "3.5.2")
endif()

hunter_cmake_args(
    hunter_venv
    CMAKE_ARGS
    HUNTER_VENV_PYTHON_VERSION=${__hunter_venv_default_python}
)

At this moment the procedure of making a relocatable Python environment is not robust (see virtualenv issue #1169). Because of that activate and deactivate scripts removed from the created environment and for other scripts shebangs set to general #!/usr/bin/env python value. It means that before running a Python script, you will have to set the PATH environment variable accordingly. As a more convenient and less error-prone approach, you can use the Python_EXECUTABLE variable:

execute_process(
    COMMAND ${Python_EXECUTABLE} -c "import sys"
    RESULT_VARIABLE result
)
execute_process(
    COMMAND ${Python_EXECUTABLE} -c "print ('Hello Hunter!')"
    RESULT_VARIABLE result
)
execute_process(
    COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/script.py
    RESULT_VARIABLE result
)

While calling find_package(hunter_venv CONFIG REQUIRED) variables Python*_FIND_REGISTRY and CMAKE_FIND_FRAMEWORK will be set to NEVER. Otherwise, find_package(Python REQUIRED) will return Python executable from the system instead of Python from created virtual environment: