tags:

views:

454

answers:

2

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.

CMake has to be able to find the path on all three major OS as I plan to deploy my application on Linux, Mac and Windows.

I tried using

include(FindPythonLibs)
find_path( PYTHON_SITE_PACKAGES site-packages ${PYTHON_INCLUDE_PATH}/.. )

however that does not work.

I can also obtain the path by running

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

on the shell, but how would I invoke that from CMake ?

SOLUTION:

Thanks, Alex. So the command that gives me the site-package dir is:

execute_process ( COMMAND python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)

The OUTPUT_STRIP_TRAILING_WHITESPACE command is needed to remove the trailing new line.

+2  A: 

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

Alex Martelli
+1  A: 

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)

Ruben