Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a directory to PYTHONPATH from CMake?

Tags:

c++

python

cmake

I have a c++ library which has a python API library based on Boost.python included in the directory structure.

module
|_python_files
|_cpp_files

Is there a standard way to configure CMakelist so that it automatically adds the library containing the python files to PYTHONPATH?

like image 556
zpasztor Avatar asked Oct 23 '25 16:10

zpasztor


2 Answers

In case you use FindPython you should be able to use

Python_add_library (my_module MODULE src1.cpp)

Or you could add the path to PYTHONPATH:

list(APPEND PYTHONPATH "${CMAKE_CURRENT_DIR}/module/python_files") 

Otherwise, you want to add python_files to your python definition path:

export PYTHONPATH="${PYTHONPATH}:../module/python_files"
like image 69
dboy Avatar answered Oct 25 '25 04:10

dboy


based on documentation this should od the trick:

set(ENV{PYTHONPATH} "${CMAKE_CURRENT_SOURCE_DIR}/module/python_files")

I didn't test that.

Note that PYTHONPATH environment variable is read and used by python interpreter not by cmake. It is used when trying locating python libraries imported by python code.

like image 45
Marek R Avatar answered Oct 25 '25 05:10

Marek R