[ACCEPTED]-Finding python site-packages directory with CMake-cmake

Accepted answer
Score: 10

Slightly updated version that I used for 4 lcm:

execute_process(
  COMMAND "${PYTHON_EXECUTABLE}" -c "if True:
    from distutils import sysconfig as sc
    print(sc.get_python_lib(prefix='', plat_specific=True))"
  OUTPUT_VARIABLE PYTHON_SITE
  OUTPUT_STRIP_TRAILING_WHITESPACE)

This sets PYTHON_SITE to the appropriate prefix-relative 3 path, suitable for use like:

install(
  FILES ${mypackage_python_files}
  DESTINATION ${PYTHON_SITE}/mypackage)

(Please don't 2 install to an absolute path! Doing so bypasses 1 CMAKE_INSTALL_PREFIX.)

Score: 8

You can execute external processes in cmake 2 with execute_process (and get the output into a variable 1 if needed, as it would be here).

Score: 8

Since CMake 3.12 you can use FindPython module which 2 populates Python_SITELIB and Python_SITEARCH variables for architecture 1 independent and specific libraries, respectively.

Example:

find_package(Python ${PYTHON_VERSION} REQUIRED COMPONENTS Development)
Python_add_library(foo MODULE
    src/foo.cc src/python_interface.cc
)
install(TARGETS foo DESTINATION ${Python_SITEARCH}/foo)
Score: 1

I suggest to use get_python_lib(True) if you are making this 4 extension as a dynamic library. This first 3 parameter should be true if you need the 2 platform specific location (in 64bit linux 1 machines, this could be /usr/lib64 instead of /usr/lib)

More Related questions