0

I git cloned pybind11's cmake exmaple. Then I built it with pip install ./cmake_example. My python file contains the following:

import cmake_example
print(cmake_example.add(1, 2))

This works fine. Now I want to use pybind11's interpreter. I changed the CMakeLists.txt according to the instructions in the docs. Below are what I have now:

main.cpp

#include <pybind11/embed.h>

namespace py = pybind11;

int main()
{
    py::scoped_interpreter guard{};

    py::print("Hello world");
}

PYBIND11_MODULE(cmake_example, m)
{
    m.def("main", &main);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project(cmake_example)

add_subdirectory(pybind11)
add_executable(cmake_example src/main.cpp)
target_link_libraries(cmake_example PRIVATE pybind11::embed)

example.py

import cmake_example
cmake_example.main()

When I run the above python file, I get the following error:

Traceback (most recent call last): File "example.py", line 2, in cmake_example.main() AttributeError: module 'cmake_example' has no attribute 'main'

What am I doing wrong?

user3132457
  • 789
  • 2
  • 11
  • 29
  • Are you sure you are importing the new module? Have you removed the previous one? – Matthieu Brucher Nov 24 '18 at 16:59
  • How should I remove it? And well, I run `pip install ./cmake_example` again, shouldn't that be enough? – user3132457 Nov 24 '18 at 17:01
  • You should check by deleting the module, just in case. – Matthieu Brucher Nov 24 '18 at 17:02
  • What do you mean by "delete the module"? Delete the "example.py" python module? By the way, in the example the function wasn't registered with `PYBIND11_MODULE`, do I need it? Also, installing the example removes the previous one: `Found existing installation: cmake-example 0.0.1 Uninstalling cmake-example-0.0.1: Successfully uninstalled cmake-example-0.0.1` – user3132457 Nov 24 '18 at 17:07

1 Answers1

0

I think you are mixing two different approaches up.

Embedding specifically refers to embed the python interpreter into an existing executable. The document that you refer to make it (or try to) quite clear.

What it means is that you should have a C/C++ executable from which you can execute python code (either inside a file or as a string).

Now that this is out of the way, look inside your built directory and you will find a cmake_example binary. Run it and you will see the print. You cannot directly import this built module from within standard python interpreter, rather it is available inside the file invoked from the custom executable, cmake_example in this case.

You can also run example.py by changing the code as following:

int main()
{
    py::scoped_interpreter guard{};

    py::eval_file("example.py");
}
Adnan Y
  • 2,982
  • 1
  • 26
  • 29
  • I run `cmake . -G"Visual Studio 14 2015 Win64"` but no executable was generated; a CMakeFiles folder is created with CMakeSystem.make and CMakeOutput.log files, that's all. – user3132457 Nov 28 '18 at 16:29
  • 1
    Just invoking cmake does not create the output files; it only creates the cache and build targets. Try running `cmake --build .` to actually build the project. – Adnan Y Nov 29 '18 at 04:52
  • Now I get an error: `Error: could not load cache`. Indeed, cache file isn't there. I tried to just `cmake .` but that didn't help. – user3132457 Nov 29 '18 at 09:06
  • Without knowing what state your project is in, what changes you have made, it is not possible to help you further. Also a simple `that didn't help` does not give enough information to guide you further. I would recommend checking out a brand new example, making changes to it and then see if it works. – Adnan Y Nov 29 '18 at 22:29
  • Okay, I was able to create the executable, but now that I run it, I get runtime error: abort() called. `example.py` is located under `src/` (where `main.cpp` is also located. Why is that? P.S. I'm using the `main()` function you wrote. – user3132457 Dec 01 '18 at 09:38
  • It's likely just the path of the script that it cannot find. Try using an absolute path such as `/home/username/cmake-example/example.py` or it's windows equivalent – Adnan Y Dec 02 '18 at 03:46