pybind / cmake_example

Example pybind11 module built with a CMake-based build system
Other
613 stars 218 forks source link

How to specify python? #122

Open Dariusz1989 opened 1 year ago

Dariusz1989 commented 1 year ago

Hello

Thanks for this awesome test! I'm lost with customization tho... How can I specify python in CMakeList.txt file? I build this file without using your python. setup routine tho... just added

add_definitions(-DVERSION_INFO=3.10)

To cmake and it build locally. Works great, locally, but I Have no idea how it knows which python to use.

I want to be explicit. Any ideas? Pinging our 2 biggest contributors hoping for a miracle :- ) @dean0x7d @henryiii

SOLVED> To specify python version using "new methods"

set(PYBIND11_PYTHON_VERSION 3.9)
set(OLD_PATH ${CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH ${P3_ROOT_DIR})
add_subdirectory(S:/pybind11 S:/pybind11)
set(CMAKE_PREFIX_PATH ${OLD_PATH})

at least on windows before adding pybind11 big thanks to @henryiii !!

henryiii commented 1 year ago

If you are building from Python, it must always be that version of Python. It's passed through via PYTHON_EXECUTABLE at https://github.com/pybind/cmake_example/blob/1017b9029e8dd62f894d84ff0b4abbe5f0bf7dd9/setup.py#L49.

I'd recommend considering FindPython over the deprecated FindPythonInterp/Libs, and checking out scikit-build-core in https://github.com/pybind/scikit_build_example over this example. You'll be stuck manually fixing problems every time setuptools updates with this example.

Dariusz1989 commented 1 year ago

Hey I've had a look at scikit but its cmake > https://github.com/pybind/scikit_build_example/blob/master/CMakeLists.txt does not specify ver, in commits I failed to find it... its probably there just not sure where... I kinda found this > https://github.com/pybind/scikit_build_example/tree/find-python but I don't see where python is specified there either... I've run this code now >

cmake_minimum_required(VERSION 3.25)
project(exampleHowToPybind)

set(PY_EXE "C:/Program Files/Python37")
set(Python3_ROOT_DIR "C:/Program Files/Python37")
set(PYTHON_EXECUTABLE "${Python3_ROOT_DIR}/python_d.exe" CACHE FILEPATH "Path to the Python executable")
set(CMAKE_FIND_DEBUG_MODE TRUE)
find_package(Python REQUIRED COMPONENTS Interpreter Development)# HINTS "${PYTHON_EXECUTABLE}")
message(STATUS ${PYTHON_DIR})

Its DEBUG build so I assume I need to use _d python ver... sadly. when I check the debug output of search he gives me this >

The item was found at
    C:/Program Files/Python310/python.exe

So he is ignoring my python executable here?

henryiii commented 1 year ago

You need to set Python_EXECUTABLE; PYTHON_EXECUTABLE is for the old search.

Dariusz1989 commented 1 year ago

T_T Yea that did the trick >.< thank you! But now I'm stuck with > S:\RnD\DLL_python\06_pybind11_include\vs\build\x64-Debug\ninja : error : '/python37.lib', needed by 'exampleHowToPybind.pyd',`` missing and no known rule to make it Strange enough hes asking for python37.lib not python37_d.lib :/ Or if I pass > ${_Python_RUNTIME_LIBRARY_RELEASE} or ${_Python_RUNTIME_LIBRARY_DEBUG} I get > C:\Program Files\Python37\python37.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2F8

Update, seems like if I just set > set(Python_EXECUTABLE "${P3_ROOT_DIR}/python.exe" CACHE FILEPATH "Path to the Python executable") and add_subdirectory(pybind11) it will use the exe set so yay! Now I just have to figure out how to do add_subdirectory without it.... off to other topic.

Summary To specify python version using "new methods" use set(Python_EXECUTABLE "path/to/python.exe") at least on windows before adding pybind11.

Dariusz1989 commented 1 year ago

Nope setting set(Python_EXECUTABLE "path/to/python.exe") does not work sadly ;[ It was a fluke :/ This is final cmd...

set(PYBIND11_PYTHON_VERSION 3.9)
set(OLD_PATH ${CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH ${P3_ROOT_DIR})
add_subdirectory(S:/pybind11 S:/pybind11)
set(CMAKE_PREFIX_PATH ${OLD_PATH})