wjakob / nanobind_example Goto Github PK
View Code? Open in Web Editor NEWA nanobind example project
License: BSD 3-Clause "New" or "Revised" License
A nanobind example project
License: BSD 3-Clause "New" or "Revised" License
Heya ๐
standard installation currently works perfectly however in editable mode I get the following:
pip install -e ./nanobind_example
python -c 'import nanobind_example'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "[...]/nanobind_example/__init__.py", line 1, in <module>
from .nanobind_example_ext import add
ModuleNotFoundError: No module named 'nanobind_example.nanobind_example_ext'
Thanks
I'm currently trying to set up CI for nanobind_bazel
, and I want to use this example as a build test.
Would it be OK for you to push a with-bazel
branch (or similar) that contains the setup for a nanobind_bazel
build of this example, which I can then use in actions/checkout
? (If not, I can also fork of course.)
Perhaps this is out of scope for this "hello world " nanobind repo, but a use case I am struggling with that is not that unconventional is that I have an external library (GEOS in this case) that I HAVE to link against dynamically due its license (LGPL v 2.1). When I was linking statically everything was peachy but now I'm in DLL hell with editable installation (which is key for quick development). The @rpath
doesn't seem to be getting set properly and it is not entirely clear to me how I should be handling this. I've tried a number of incantations of CMAKE rpath messing about but so far I still can't load both my nanobind main repo and the dependent library.
After the nanobind_example is build, I want to copy some external dlls to the final destination folder. However,
add_custom_command(TARGET nanobind_example_ext POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"D:/my_folder/some_lib.dll"
$<TARGET_FILE_DIR:nanobind_example_ext>)
is not working. How to fix this?
Hey ๐๐ผ
First of all, many thanks for creating this project. Really great to see how nanobind and scikit-build-core come together.
I am currently in the process of migrating some of code-bases from pybind11+setuptools to nanobind+scikit-build-core.
Most of the setup was surprisingly easy (props to that). However, I am facing some peculiar problems with editable installs.
After setting up the project and performing an editable install via
git clone https://github.com/wjakob/nanobind_example.git
cd nanobind_example
python3 -m venv .venv
source .venv/bin/activate
pip install nanobind scikit-build-core[pyproject]
pip install --no-build-isolation -ve .
my IDE can't seem to find the definition for add
in the nanobind_example
package. And highlights it with
Cannot find reference 'add' in 'imported module nanobind_example | __init__.py'
After some digging, I think I found out, why that might be the case. Even in editable mode, the shared nanobind_example_ext
library gets installed into the site-packages
directory (under the nanobind_example
folder).
However, since an editable install was performed, the site-packages
directory does not contain the __init__.py
file from the package directory.
An IDE like PyCharm or CLion will notice the site-packages/nanobind_example
folder and consider that for looking up definitions. Since that directory does not contain the __init__.py
file, it just shows the above error message.
In my past projects using setuptools and pybind11, an editable install always led to the shared library being copied to the in-source package directory. In this case, that would be src/nanobind_example
. No package directory was created in site-packages
. In that fashion, IDEs were always able to pick up the proper definitions.
Long story short: It seems unintended to me that an editable install installs something to site-packages
and it kind of breaks IDEs. I would have expected the editable install to install the shared library to the source tree and not to site-packages
.
Tagging @henryiii since I am not quite sure whether this is something that can be resolved at the nanobind
side or the scikit-build-core
side.
Probably related: scikit-build/scikit-build-core#374
Another side-effect that is kind of related to the above issue:
Due to the use of a relative import in
from nanobind_example.nanobind_example_ext import add
This works both for a standard installation and in editable mode. Happy to submit a PR if you agree.
The problem is simple. When we free a vector (e.g., by replacing it with another vector), python objects that refer to elements in that vector become "wild pointers".
I verified my suspicions with the following code. I'd like to know if there's a way to avoid this problem, or at the very least get a warning when I access invalid python objects, because accessing these "wild pointers" has a probability of giving the program a segmentation fault.
Here is the minimal code:
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/bind_vector.h>
#include <vector>
#include <string>
namespace nb = nanobind;
struct Bar {
int data;
explicit Bar (const int data) : data(data) {}
};
struct Foo {
std::vector<Bar> bars;
explicit Foo(std::vector<Bar> bars) : bars(std::move(bars)) {}
};
NB_MAKE_OPAQUE(std::vector<Bar>);
NB_MODULE(nanobind_example_ext, m) {
nb::class_<Bar>(m, "Bar")
.def(nb::init<int>())
.def_rw("data", &Bar::data)
.def("__repr__", [](const Bar& self) { return "Bar(" + std::to_string(self.data) + ")"; });
nb::bind_vector<std::vector<Bar>>(m, "BarList");
nb::class_<Foo>(m, "Foo")
.def(nb::init<std::vector<Bar>>())
.def_rw("bars", &Foo::bars)
.def("__repr__", [](const Foo& self) { return "Foo(" + std::to_string(self.bars.size()) + " bars)"; });
}
# in ipython
from nanobind_example.nanobind_example_ext import Foo, Bar, BarList
f = Foo(BarList([Bar(i) for i in range(10000)]))
b = f.bars[-1]
print(b, id(b)
%%timeit
f.bars = BarList(Bar(0) for _ in range(10000))
print(b, id(b)
Are you interested in a PR that adds single-sourcing the version and creates a __version__
attribute in the compiled extension to mirror other libraries like numpy, scipy, etc.?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.