Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError for newer pybind11 versions #36

Closed
h-larsson opened this issue Apr 8, 2023 · 6 comments
Closed

ImportError for newer pybind11 versions #36

h-larsson opened this issue Apr 8, 2023 · 6 comments

Comments

@h-larsson
Copy link
Contributor

Hi Huanchen,

It looks like Pybind Version >= 2.10.2 introduces the error

ImportError: generic_type: cannot initialize type "KeysView[Tuple[str, int]]": an object with that name is already defined

Which seems to be due to pybind/pybind11#4353
Do you have an idea how to fix this?

@hczhai
Copy link
Contributor

hczhai commented Apr 8, 2023

Yes that's why I restricted the version number here: https://github.com/block-hczhai/block2-preview/blob/master/setup.py#L170
I will have a look at the link and see if I can fix it.

@hczhai
Copy link
Contributor

hczhai commented Apr 8, 2023

Hi, I think this is a bug introduced in pybind11==2.10.2 due to the pull request you mentioned, and it is not solved even in the newest version pybind11==2.10.4. You can get the error using the following minimal example:

#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
#include <map>
#include <string>

namespace py = pybind11;
using namespace std;

PYBIND11_MODULE(xtest, m) {
    m.doc() = "xtest.";
    py::bind_map<map<int, int>>(m, "xx");
    py::bind_map<map<long long int, int>>(m, "yy");
}

Save this file as xtest.cpp and you can compile it using

g++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) xtest.cpp -o xtest$(python3-config --extension-suffix)

And then if you do python -c 'import xtest' you get the following

ImportError: generic_type: cannot initialize type "KeysView[int]": an object with that name is already defined

Now if you change the two lines to

    py::bind_map<map<int, double>>(m, "xx");
    py::bind_map<map<long long int, int>>(m, "yy");

You get even strange warnings at compile time. So there is nothing I can do to fix this. Let's just give up the version >=2.10.2.

@hczhai
Copy link
Contributor

hczhai commented Apr 8, 2023

This is basically pybind/pybind11#4529

@h-larsson
Copy link
Contributor Author

Thanks for checking this. Hopefully they will resolve it soon.

@Tobias-Fischer
Copy link

Have you found a workaround for this?

@hczhai
Copy link
Contributor

hczhai commented Jan 19, 2024

@Tobias-Fischer @h-larsson The issue was fixed in pybind/pybind11#4985.

@hczhai hczhai closed this as completed Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants