# FindPython3: how to specify local installation directory for Python module?

**URL:** https://discourse.cmake.org/t/findpython3-how-to-specify-local-installation-directory-for-python-module/3580
**Category:** Code
**Created:** [June 16, 2021, 9:12pm UTC](https://discourse.cmake.org/t/findpython3-how-to-specify-local-installation-directory-for-python-module/3580 "2021-06-16T21:12:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jwuttke](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jwuttke](https://discourse.cmake.org/u/jwuttke)
#### Post date: [June 16, 2021, 9:12pm UTC](https://discourse.cmake.org/t/findpython3-how-to-specify-local-installation-directory-for-python-module/3580/1 "2021-06-16T21:12:51Z")

</div>

```auto
include(GNUInstallDirs)
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
...
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mymodule.py
          DESTINATION ${Python3_SITELIB})

```

installs to `/usr/lib/python3/dist-packages/`.

How to install to `/usr/local/lib/python3/dist-packages/`?

Is there a way to base the solution on `${CMAKE_INSTALL_LIBDIR}`?

---

<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, 12:18am UTC](https://discourse.cmake.org/t/findpython3-how-to-specify-local-installation-directory-for-python-module/3580/2 "2021-06-17T00:18:09Z")

</div>

I usually do something like:

```cmake
if (NOT CMAKE_INSTALL_PYTHON_LIBDIR)
  set(CMAKE_INSTALL_PYTHON_LIBDIR
    "${CMAKE_INSTALL_LIBDIR}/python${major}.${minor}/site-packages")
endif ()

```

Distributors can then override with whatever their preferred layout is. And whatever the spelling for the `major` and `minor` variables are.

---

<div class="post-metadata">

### Author: ![jwuttke](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jwuttke](https://discourse.cmake.org/u/jwuttke)
#### Post date: [June 17, 2021, 5:35am UTC](https://discourse.cmake.org/t/findpython3-how-to-specify-local-installation-directory-for-python-module/3580/3 "2021-06-17T05:35:32Z")

</div>

The variable `CMAKE_INSTALL_PYTHON_LIBDIR` is meant to be set by the user? It is not defined by CMake? Shouldn’t it be better called `<my_project>_INSTALL_PYTHON_LIBDIR`?

The version variables are defined by [FindPython3](https://cmake.org/cmake/help/v3.20/module/FindPython3.html).

---

<div class="post-metadata">

### Author: ![jwuttke](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jwuttke](https://discourse.cmake.org/u/jwuttke)
#### Post date: [June 17, 2021, 7:18am UTC](https://discourse.cmake.org/t/findpython3-how-to-specify-local-installation-directory-for-python-module/3580/4 "2021-06-17T07:18:25Z")

</div>

Promising idea from [https://stackoverflow.com/a/40006251/1017348:](https://stackoverflow.com/a/40006251/1017348:)

```auto
  COMMAND "${Python3_EXECUTABLE}" -c "from distutils import sysconfig as sc;print(sc.get_python_lib(prefix='', plat_specific=True))"
  OUTPUT_VARIABLE PYTHON_SITE
  OUTPUT_STRIP_TRAILING_WHITESPACE)

```

Under Debian this yields `PYTHON_SITE=lib/python3/dist-packages`. A relative path, since the prefix is supplied by `CMAKE_INSTALL_PREFIX=/usr/local`. Unfortunately though, `/usr/local/lib/python3/dist-packages` is not in `sys.path`. We’d rather need `/usr/local/lib/python3.${minor}/dist-packages`.

So the above is close, but not quite the solution.

---

<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, 1:14pm UTC](https://discourse.cmake.org/t/findpython3-how-to-specify-local-installation-directory-for-python-module/3580/5 "2021-06-17T13:14:06Z")

</div>

> [@jwuttke](#):
>
> `<my_project>_INSTALL_PYTHON_LIBDIR`

I named it that in order to follow `GNUInstallDirs` naming patterns (it keeps all of the destinations beside each other in the cache). And yes, if the default is not sufficient, the user should set it. You can use fancier logic to get the default (such as the Python code from your SO link).

> [@jwuttke](#):
>
> The version variables are defined by [FindPython3](https://cmake.org/cmake/help/v3.20/module/FindPython3.html).

Yes, I’m aware, I was just being lazy 🙂 .
