wjakob / nanobind

nanobind: tiny and efficient C++/Python bindings
BSD 3-Clause "New" or "Revised" License
2.29k stars 191 forks source link

[BUG]: Ubuntu, CMake, .so file is built but no python library. #503

Closed petrasvestartas closed 5 months ago

petrasvestartas commented 6 months ago

Problem description

This is my CMakeLists.txt that downloads Nanobind and build the main.cpp example file with the documentation sourcecode. Why does cmake output NanoTest.cpython-311-x86_64-linux-gnu.so but not the .pyd file?

image

This is my sourcecode:

cmake_minimum_required(VERSION 3.5)
project(TestProject)
include(ExternalProject)
option(DOWNLOAD_DEPENDECIES ON)
option(BUILD OFF)

set(CMAKE_CXX_STANDARD 14)

if(DOWNLOAD_DEPENDECIES)
    ExternalProject_Add(Nanobind
    GIT_REPOSITORY https://github.com/wjakob/nanobind.git
    GIT_TAG master
    PREFIX ${CMAKE_BINARY_DIR}/nanobind
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    LOG_DOWNLOAD ON
    )
endif()

if(BUILD)

    find_package(Python 3.9 COMPONENTS Interpreter Development.Module REQUIRED)

    if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
        set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
        set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
    endif()

    include(${CMAKE_BINARY_DIR}/nanobind/src/Nanobind/cmake/nanobind-config.cmake)
    nanobind_add_module(NanoTest libShapeOp/examples/main.cpp)

endif()

I run it like this:

#!/bin/bash

mkdir -p build
cd build
cmake -B . -S .. -DDOWNLOAD_DEPENDECIES=ON -DBUILD=OFF -G "Unix Makefiles" && make
cmake -B . -S .. -DDOWNLOAD_DEPENDECIES=OFF -DBUILD=ON -G "Unix Makefiles" && make

Reproducible example code

No response

a603032070 commented 5 months ago

".so"(on linux) and ".pyd"(on windows) files are basically the same. Have you tried import NanoTest?

petrasvestartas commented 5 months ago

@a603032070 Yes I tried, no module found. I changed binding name to my_ext to be inline with the example. I checked on my system I have python 3.11.

I also tried to give a path directly to the .so file:

import sys
sys.path.append('/home/petras/brg/2_code/ShapeOp/build/')
import my_ext

Still the same error.

I removed any 3rd party libraries and just left nano, could you check my repository: https://github.com/petrasvestartas/ShapeOp

a603032070 commented 5 months ago

@a603032070 Yes I tried, no module found. I changed binding name to my_ext to be inline with the example. I checked on my system I have python 3.11.

I also tried to give a path directly to the .so file:

import sys
sys.path.append('/home/petras/brg/2_code/ShapeOp/build/')
import my_ext

Still the same error.

I removed any 3rd party libraries and just left nano, could you check my repository: https://github.com/petrasvestartas/ShapeOp

I've checked your repo. It works well. Maybe something wrong about the path(or permissions)?

petrasvestartas commented 5 months ago

@a603032070

The issue is in python version. In my base environment with python 3.11 it works. But with my conda environment where python is 3.9 it does not.

How you can specify in CMakelists which python version the library is built for?

a603032070 commented 5 months ago

I don't really know. Maybe it's about this line in Cmakelists. find_package(Python 3.9 COMPONENTS Interpreter Development.Module REQUIRED)

https://cmake.org/cmake/help/latest/module/FindPython.html

wjakob commented 5 months ago

ExternalProject_Add is not a supported way of dealing with the nanobind dependency, I don't use it and cannot provide support for it.

I also suspect you are working partly on windows and on Linux (WSL?), hence the confusion about .pyd and .so files which are for different operating systems.

Anyways, these are all not bugs bug issues with your setup. Please don't open tickets here unless you are certain to have found a bug in nanobind.