pybind11 cmake example cannot find the main function












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?










share|improve this question























  • 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


















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?










share|improve this question























  • 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
















0












0








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?










share|improve this question














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?







python c++ cmake pybind11 language-binding






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 16:52









user3132457user3132457

1221311




1221311













  • 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





















  • 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



















Are you sure you are importing the new module? Have you removed the previous one?

– Matthieu Brucher
Nov 24 '18 at 16:59





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





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





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







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 Answer
1






active

oldest

votes


















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");
}





share|improve this answer
























  • 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 4: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 9: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 1 '18 at 9:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460368%2fpybind11-cmake-example-cannot-find-the-main-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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");
}





share|improve this answer
























  • 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 4: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 9: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 1 '18 at 9:38
















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");
}





share|improve this answer
























  • 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 4: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 9: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 1 '18 at 9:38














0












0








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");
}





share|improve this answer













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");
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 7:52









Adnan YAdnan Y

1,80711323




1,80711323













  • 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 4: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 9: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 1 '18 at 9:38



















  • 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 4: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 9: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 1 '18 at 9:38

















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





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




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 4:52







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 4: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 9:06







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 9: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





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 1 '18 at 9:38





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 1 '18 at 9:38




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460368%2fpybind11-cmake-example-cannot-find-the-main-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'