Finding python site-packages directory with CMake

前端 未结 4 1048
野的像风
野的像风 2020-12-31 05:29

I use CMake to build my application. How can I find where the python site-packages directory is located? I need the path in order to compile an extension to python.

相关标签:
4条回答
  • 2020-12-31 05:45

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

    0 讨论(0)
  • 2020-12-31 05:46

    Since CMake 3.12 you can use FindPython module which populates Python_SITELIB and Python_SITEARCH variables for architecture 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)
    
    0 讨论(0)
  • 2020-12-31 06:01

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

    0 讨论(0)
  • 2020-12-31 06:02

    Slightly updated version that I used for 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 path, suitable for use like:

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

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

    0 讨论(0)
提交回复
热议问题