# Finding pyenv python package using CMake

**URL:** https://discourse.cmake.org/t/finding-pyenv-python-package-using-cmake/3586
**Category:** Usage
**Created:** [June 17, 2021, 4:50pm UTC](https://discourse.cmake.org/t/finding-pyenv-python-package-using-cmake/3586 "2021-06-17T16:50:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Bernstein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/david_bernstein/32/1568_2.png) [@David\_Bernstein](https://discourse.cmake.org/u/David_Bernstein)
#### Post date: [June 17, 2021, 4:50pm UTC](https://discourse.cmake.org/t/finding-pyenv-python-package-using-cmake/3586/1 "2021-06-17T16:50:42Z")

</div>

Hi, We are using pyenv to manage python versions in a project that mixes python and C++ code. We have one item whose CMakeLists file needs to find the python package which is specified by the project pyenv version. Right now it finds the system version instead. What is the cmake command to find the python interpreter for the version specified by pyenv? Ubuntu 18.04, cmake version 3.10 (or newer is ok), the python pyenv version is 3.7.3.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 17, 2021, 5:20pm UTC](https://discourse.cmake.org/t/finding-pyenv-python-package-using-cmake/3586/2 "2021-06-17T17:20:38Z")

</div>

I suspect that specifying the search strategy and a `ROOT` variable based on the `pyenv` information CMake has access to. See the [`FindPython3` docs](https://cmake.org/cmake/help/latest/module/FindPython3.html) for details and the variables that matter.

---

<div class="post-metadata">

### Author: ![itavero](https://discourse.cmake.org/user_avatar/discourse.cmake.org/itavero/32/2547_2.png) [@itavero](https://discourse.cmake.org/u/itavero)
#### Post date: [May 28, 2025, 2:59pm UTC](https://discourse.cmake.org/t/finding-pyenv-python-package-using-cmake/3586/3 "2025-05-28T14:59:46Z")

</div>

A bit late to the party, but I ran into issues with CMake + pyenv on Windows today.  
Based on the comment above, I added a few lines before calling `find_package` to set the `ROOT_DIR`, which solved the issue for me.

```auto
# Before looking for Python, check if pyenv is available
if(WIN32)
  find_program(PYENV_EXE NAMES pyenv.bat pyenv)

  if(PYENV_EXE)
    execute_process(
      COMMAND "${PYENV_EXE}" which python3
      OUTPUT_VARIABLE PYENV_PYTHON
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )

    if(PYENV_PYTHON AND EXISTS "${PYENV_PYTHON}")
      get_filename_component(Python3_ROOT_DIR "${PYENV_PYTHON}" DIRECTORY)
      set(Python3_ROOT_DIR "${Python3_ROOT_DIR}" CACHE PATH "Root dir for Python3 from pyenv")
      message(STATUS "Used pyenv to find Python 3 in: ${Python3_ROOT_DIR}")
    endif()
  endif()
endif()

find_package(Python3 REQUIRED)

```
