# troubles with find\_package and CMAKE\_PREFIX\_PATH

**URL:** <https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [November 11, 2023, 7:38pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414 "2023-11-11T19:38:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [November 11, 2023, 7:38pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/1 "2023-11-11T19:38:38Z")

</div>

Hello,

i have the problem that i’ve build cpython on my own in /home/stuv/build/cpython and i set CMAKE\_PREFIX\_PATH to this path but when i try

find\_package(  
Python3  
REQUIRED VERSION 3.13  
COMPONENTS Development  
)

he tells me he can only find the version 3.11 which is the system installed one. is there a way i can tell cmake which version i wold prefer or why he doesn’t he find my version from the build dir ?

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [November 11, 2023, 10:05pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/2 "2023-11-11T22:05:08Z")

</div>

Maybe you have to install your build (make install) to have a correct layout to be searchable…

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [November 12, 2023, 9:04am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/3 "2023-11-12T09:04:18Z")

</div>

i’ve done so now with --prefix and put the builded version into /home/user/build/python3.13 and my code looks like:  
cmake\_minimum\_required(VERSION 3.5)  
project(TestPython)

set(EXTERNAL\_BUILD\_PATH /home/user/build/python3.13)  
list(APPEND CMAKE\_PREFIX\_PATH ${EXTERNAL\_BUILD\_PATH})  
find\_package(  
Python  
version 3.13  
REQUIRED  
PATHS ${EXTERNAL\_BUILD\_PATH}  
)

if(Python\_FOUND)  
message(“python was found”)  
elseif()  
message(“python was not found”)  
endif()

and it gives me the error message find\_package called with invalid argument “version” …

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [November 12, 2023, 9:33am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/4 "2023-11-12T09:33:55Z")

</div>

As the error message said, `version` is not a valid argument to [find\_package()](https://cmake.org/cmake/help/latest/command/find_package.html) command. The version can be specified but without keyword:

```cmake
find_package(Python 
3.13
REQUIRED
PATHS ${EXTERNAL_BUILD_PATH}
)

```

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [November 12, 2023, 5:30pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/5 "2023-11-12T17:30:01Z")

</div>

i now get the output:  
CMake Error at /usr/local/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:230 (message):  
Could NOT find Python3 (missing: PATH /home/stuv/build/python3.13) (found  
suitable version “3.13.0”, minimum required is “3.13.0”)  
Call Stack (most recent call first):  
/usr/local/share/cmake-3.28/Modules/FindPackageHandleStandardArgs.cmake:600 (\_FPHSA\_FAILURE\_MESSAGE)  
/usr/local/share/cmake-3.28/Modules/FindPython/Support.cmake:3867 (find\_package\_handle\_standard\_args)  
/usr/local/share/cmake-3.28/Modules/FindPython3.cmake:545 (include)  
CMakeLists.txt:56 (find\_package)

so it looks like he can find the version but has some othe trouble, i set Python\_DIR to /home/user/build/python3.13

---

<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:** [November 17, 2023, 3:26pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/6 "2023-11-17T15:26:32Z")

</div>

Configuring with the `--debug-find` may help figure out why that `PATH` bit is considered missing.

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 18, 2023, 10:59am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/7 "2023-12-18T10:59:52Z")

</div>

Thank you for the parameter but the directory is not listed but i pass it to MCMAKE\_PREFIX\_PATH:

```auto
function(bs_set_cmake_prefix_path)
    set(CMAKE_PREFIX_QT6 "/home/stuv/Qt/6.6.1/gcc_64/lib/cmake/Qt6")
    set(CMAKE_PREFIX_PYTHON3 "/home/stuv/projects/build/python3.13")
    set(QT6_RPATH "/home/stuv/Qt/6.6.1/gcc_64/lib")

    list(APPEND CMAKE_PREFIX_PATH ${CMAKE_PREFIX_QT6} ${CMAKE_PREFIX_PYTHON3})
endfunction()

```

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [December 18, 2023, 11:09am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/8 "2023-12-18T11:09:56Z")

</div>

CMake functions establish a variable scope. When you modify `CMAKE_PREFIX_PATH` inside a function, the modification will not propagate outside the function. See [Introduction to `list()` command](https://cmake.org/cmake/help/latest/command/list.html#introduction) for details and possible solutions.

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 18, 2023, 11:19am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/9 "2023-12-18T11:19:22Z")

</div>

list(APPEND PARENT\_SCOPE CMAKE\_PREFIX\_PATH ${CMAKE\_PREFIX\_QT6} ${CMAKE\_PREFIX\_PYTHON3}) does not work

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [December 18, 2023, 11:21am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/10 "2023-12-18T11:21:06Z")

</div>

And nothing in the docs suggests that it should. Quoting the docs I linked:

> To propagate the results of these operations upwards, use [`set()`](https://cmake.org/cmake/help/latest/command/set.html#command:set) with `PARENT_SCOPE`, [`set()`](https://cmake.org/cmake/help/latest/command/set.html#command:set) with `CACHE INTERNAL`, or some other means of value propagation.

Have you tried one of these?

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 18, 2023, 11:28am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/11 "2023-12-18T11:28:43Z")

</div>

when i copy the text from the .cmake file to the main CMakeLists.txt file it doe work!!

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 18, 2023, 11:34am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/12 "2023-12-18T11:34:32Z")

</div>

gut when i remove it from the CMakeLists.txt and use:

```auto
function(bs_set_cmake_prefix_path)
    set(CMAKE_PREFIX_QT6 "/home/stuv/Qt/6.6.1/gcc_64/lib/cmake/Qt6" PARENT_SCOPE)
    set(CMAKE_PREFIX_PYTHON3 "/home/stuv/projects/build/python3.13" PARENT_SCOPE)
    set(QT6_RPATH "/home/stuv/Qt/6.6.1/gcc_64/lib" PARENT_SCOPE)

    list(APPEND CMAKE_PREFIX_PATH ${CMAKE_PREFIX_QT6} ${CMAKE_PREFIX_PYTHON3} PARENT_SCOPE)
endfunction()

```

The directory is not listet with --find-debug

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [December 18, 2023, 11:36am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/13 "2023-12-18T11:36:33Z")

</div>

`list()` doesn’t support `PARENT_SCOPE`. Please try reading through the linked docs a bit.

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 18, 2023, 11:39am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/14 "2023-12-18T11:39:33Z")

</div>

ups, thank you

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 18, 2023, 12:03pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/15 "2023-12-18T12:03:19Z")

</div>

i now tried it with block() but this also doesn’t work like expected:

```auto
function(bs_set_cmake_prefix_path)
  block(PROPAGATE CMAKE_PREFIX_PATH CMAKE_PREFIX_QT6 CMAKE_PREFIX_PYTHON3 QT6_RPATH)
    set(CMAKE_PREFIX_PYTHON3 "/home/stuv/projects/build/python3.13")
    set(CMAKE_PREFIX_QT6 "/home/stuv/Qt/6.6.1/gcc_64/lib/cmake/Qt6")
    set(QT6_RPATH "/home/stuv/Qt/6.6.1/gcc_64/lib")
    set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_QT6} ${CMAKE_PREFIX_PYTHON3})
  endblock()

endfunction()

```

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [December 18, 2023, 1:23pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/16 "2023-12-18T13:23:22Z")

</div>

Indeed. `block()` introduces its own scope, as does the function. So your code propagates the variables out of the block into the enclosing scope, which is the function. You need to do the modifications such that they propagate outside of the function.

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 18, 2023, 2:09pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/17 "2023-12-18T14:09:35Z")

</div>

i’ve tried now the block() command like:

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 18, 2023, 2:16pm UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/18 "2023-12-18T14:16:11Z")

</div>

I changed the list() to set() and used block() like:

---

<div class="post-metadata">

**Author:** ![bytecounter](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/4491bb/32.png) [@bytecounter](https://discourse.cmake.org/u/bytecounter)\
**Post date:** [December 19, 2023, 4:32am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/19 "2023-12-19T04:32:00Z")

</div>

It’s strange, i’ve a function where i set my variables with PARENT\_SCOPE like this:

```auto
function(bs_set_cmake_prefix_path)
  set(CMAKE_PREFIX_PYTHON3 "/home/stuv/projects/build/python3.13" PARENT_SCOPE)
  set(CMAKE_PREFIX_QT6 "/home/stuv/Qt/6.6.1/gcc_64/lib/cmake/Qt6" PARENT_SCOPE)
  set(QT6_RPATH "/home/stuv/Qt/6.6.1/gcc_64/lib" PARENT_SCOPE)
  set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_QT6} ${CMAKE_PREFIX_PYTHON3} PARENT_SCOPE)
endfunction()

```

When i put a message() after the function all variables are in the parent scope, only CMAKE\_PREFIX\_PATH is empty!

---

<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:** [December 19, 2023, 4:36am UTC](https://discourse.cmake.org/t/troubles-with-find-package-and-cmake-prefix-path/9414/20 "2023-12-19T04:36:55Z")

</div>

`PARENT_SCOPE` is weird[1]. For a function like that, I would recommend removing `PARENT_SCOPE` and just using a `macro` instead.

[1] The detail that you’re missing (and it’s a silly behavior, but it’s what we have) is that the local variable of the same name is pulled from the parent scope. So you set your variable _there_, but _here_ the value is still “undefined”, so when you try to set up `CMAKE_PREFIX_PATH`, the element variables you’re using are empty.
