Does CMake support Python3?
Asked Answered
D

3

18

I am not able to build a Python wrapper module for my C library via CMake and Swig for Python3. Everything works fine for Python2.x but it looks like CMake cannot find Python3. I already looked around and tried a couple of things.

For example, my python executable links to Python3, as I read CMake will find this version first.

Please see here the SWIG part of the CMakeLists.txt:

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PACKAGE(PythonInterp 3)
FIND_PACKAGE(PythonLibs 3)

FIND_PATH(PYTHON_INCLUDE_PATH Python.h
  /usr/include
  /usr/local/include)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(kissCT3.i PROPERTIES CPLUSPLUS ON)
#SET_SOURCE_FILES_PROPERTIES(kissCT3.i PROPERTIES SWIG_FLAGS "-includeall -py3")

SWIG_ADD_MODULE(kissCT3 python kissCT3.i)
SWIG_LINK_LIBRARIES(kissCT3 libct2d matio kissfft ${PYTHON_LIBRARIES})

Unfortunately, the output after calling cmake shows that only python2.7 is found:

-- Found SWIG: /usr/bin/swig2.0 (found version "2.0.4")
-- Found PythonInterp: /usr/bin/python2.7 (Required is at least version "3")
-- Found PythonLibs: /usr/lib/libpython2.7.so (Required is at least version "3")
-- Configuring done
-- Generating done
Dudeen answered 17/7, 2013 at 8:26 Comment(2)
I was going to suggest that you make your own version of the finder module for python that supports python3 and put that in the same folder as your CMakelists.txt however it appears that cmake-2.8.11.X has direct support for python3. Do you have some old version of cmake?Unwieldy
I am using cmake version 2.8.5. So, this might be the problem, I will look into it, thanks!Dudeen
P
9

Can you make sure your build directory is clean? I had the exact same issue and after cleaning the dir it worked.

Purview answered 3/12, 2013 at 18:21 Comment(2)
This fixed the problem for me, too. Thanks.Mettle
Worked perfectly!Expediential
D
6

CMake 3.12 should help you with your issue. Citing the release notes:

New “FindPython3” and “FindPython2” modules, as well as a new “FindPython” module, have been added to provide a new way to locate python environments.

Divertissement answered 15/6, 2018 at 22:55 Comment(0)
D
0

I kind of solved the problem by setting the python version manually. This is not really recommendable but it works for now (note that you have to use your own installation path of the python version you want to use):

SET(PYTHON_INCLUDE_PATH /usr/include/python3.2mu)
SET(PYTHON_LIBRARIES /usr/lib/libpython3.2mu.so)
SET(PYTHON_EXECUTABLE /usr/bin/python3.2mu)
SET(PYTHON_INCLUDE_DIR  /usr/include/python3.2mu)
Dudeen answered 17/7, 2013 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.