# 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:** 1
**Showing post:** 3

<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)

```

---

_[View the full topic](https://discourse.cmake.org/t/finding-pyenv-python-package-using-cmake/3586)._
