Embedding pybind11 with virtual environment












2















I'm trying to embed python using pybind11 into my C++ application. Using the following CMake property, I managed to compile against a virtual environment of my project.



-DPYTHON_EXECUTABLE:FILEPATH=C:/Python/Envs/myproject/Scripts/python.exe


When I run the application I get an error (below) without a specific error. However I assume it fails to load the module numpy which I'm loading.




abort() has been called




#include <iostream>
#include <pybind11/embed.h>

namespace py = pybind11;

int main() {
py::scoped_interpreter guard{};
auto sys = py::module::import("sys");
py::print("Hello, World from Python!");
py::print(sys.attr("executable"));
py::print(sys.attr("version"));
// works until here

auto np = py::module::import("numpy");
py::print(np.attr("version"));

return EXIT_SUCCESS;
}


If I only import the sys module (which is in the standard library) the application works fine. This is the output of the application until the crash:



Hello, World from Python!
C:Developsandboxpython_bindingcmake-build-debugbinpython_binding.exe
3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]


I've added the following directories to my PATH.




PATH=C:PythonEnvsprojectScripts;C:PythonEnvsprojectLib




Do I have to specify the paths to other parts of the python interpreter I'm using or could this be caused by another problem?










share|improve this question























  • Have you specified PYTHONPATH properly for your venv?

    – Matthieu Brucher
    Nov 26 '18 at 11:31











  • I had to set the PYTHONPATH to site-packages set PYTHONPATH=C:PythonEnvsprojectLibsite-packages

    – ipa
    Nov 26 '18 at 15:34


















2















I'm trying to embed python using pybind11 into my C++ application. Using the following CMake property, I managed to compile against a virtual environment of my project.



-DPYTHON_EXECUTABLE:FILEPATH=C:/Python/Envs/myproject/Scripts/python.exe


When I run the application I get an error (below) without a specific error. However I assume it fails to load the module numpy which I'm loading.




abort() has been called




#include <iostream>
#include <pybind11/embed.h>

namespace py = pybind11;

int main() {
py::scoped_interpreter guard{};
auto sys = py::module::import("sys");
py::print("Hello, World from Python!");
py::print(sys.attr("executable"));
py::print(sys.attr("version"));
// works until here

auto np = py::module::import("numpy");
py::print(np.attr("version"));

return EXIT_SUCCESS;
}


If I only import the sys module (which is in the standard library) the application works fine. This is the output of the application until the crash:



Hello, World from Python!
C:Developsandboxpython_bindingcmake-build-debugbinpython_binding.exe
3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]


I've added the following directories to my PATH.




PATH=C:PythonEnvsprojectScripts;C:PythonEnvsprojectLib




Do I have to specify the paths to other parts of the python interpreter I'm using or could this be caused by another problem?










share|improve this question























  • Have you specified PYTHONPATH properly for your venv?

    – Matthieu Brucher
    Nov 26 '18 at 11:31











  • I had to set the PYTHONPATH to site-packages set PYTHONPATH=C:PythonEnvsprojectLibsite-packages

    – ipa
    Nov 26 '18 at 15:34
















2












2








2








I'm trying to embed python using pybind11 into my C++ application. Using the following CMake property, I managed to compile against a virtual environment of my project.



-DPYTHON_EXECUTABLE:FILEPATH=C:/Python/Envs/myproject/Scripts/python.exe


When I run the application I get an error (below) without a specific error. However I assume it fails to load the module numpy which I'm loading.




abort() has been called




#include <iostream>
#include <pybind11/embed.h>

namespace py = pybind11;

int main() {
py::scoped_interpreter guard{};
auto sys = py::module::import("sys");
py::print("Hello, World from Python!");
py::print(sys.attr("executable"));
py::print(sys.attr("version"));
// works until here

auto np = py::module::import("numpy");
py::print(np.attr("version"));

return EXIT_SUCCESS;
}


If I only import the sys module (which is in the standard library) the application works fine. This is the output of the application until the crash:



Hello, World from Python!
C:Developsandboxpython_bindingcmake-build-debugbinpython_binding.exe
3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]


I've added the following directories to my PATH.




PATH=C:PythonEnvsprojectScripts;C:PythonEnvsprojectLib




Do I have to specify the paths to other parts of the python interpreter I'm using or could this be caused by another problem?










share|improve this question














I'm trying to embed python using pybind11 into my C++ application. Using the following CMake property, I managed to compile against a virtual environment of my project.



-DPYTHON_EXECUTABLE:FILEPATH=C:/Python/Envs/myproject/Scripts/python.exe


When I run the application I get an error (below) without a specific error. However I assume it fails to load the module numpy which I'm loading.




abort() has been called




#include <iostream>
#include <pybind11/embed.h>

namespace py = pybind11;

int main() {
py::scoped_interpreter guard{};
auto sys = py::module::import("sys");
py::print("Hello, World from Python!");
py::print(sys.attr("executable"));
py::print(sys.attr("version"));
// works until here

auto np = py::module::import("numpy");
py::print(np.attr("version"));

return EXIT_SUCCESS;
}


If I only import the sys module (which is in the standard library) the application works fine. This is the output of the application until the crash:



Hello, World from Python!
C:Developsandboxpython_bindingcmake-build-debugbinpython_binding.exe
3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]


I've added the following directories to my PATH.




PATH=C:PythonEnvsprojectScripts;C:PythonEnvsprojectLib




Do I have to specify the paths to other parts of the python interpreter I'm using or could this be caused by another problem?







python c++ pybind11






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 11:26









ipaipa

1,01811331




1,01811331













  • Have you specified PYTHONPATH properly for your venv?

    – Matthieu Brucher
    Nov 26 '18 at 11:31











  • I had to set the PYTHONPATH to site-packages set PYTHONPATH=C:PythonEnvsprojectLibsite-packages

    – ipa
    Nov 26 '18 at 15:34





















  • Have you specified PYTHONPATH properly for your venv?

    – Matthieu Brucher
    Nov 26 '18 at 11:31











  • I had to set the PYTHONPATH to site-packages set PYTHONPATH=C:PythonEnvsprojectLibsite-packages

    – ipa
    Nov 26 '18 at 15:34



















Have you specified PYTHONPATH properly for your venv?

– Matthieu Brucher
Nov 26 '18 at 11:31





Have you specified PYTHONPATH properly for your venv?

– Matthieu Brucher
Nov 26 '18 at 11:31













I had to set the PYTHONPATH to site-packages set PYTHONPATH=C:PythonEnvsprojectLibsite-packages

– ipa
Nov 26 '18 at 15:34







I had to set the PYTHONPATH to site-packages set PYTHONPATH=C:PythonEnvsprojectLibsite-packages

– ipa
Nov 26 '18 at 15:34














1 Answer
1






active

oldest

votes


















1














I had to set the PYTHONPATH to site-packages



set PYTHONPATH=C:PythonEnvsprojectLibsite-packages


instead of C:/Python/Envs/project/






share|improve this answer
























    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%2f53480120%2fembedding-pybind11-with-virtual-environment%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









    1














    I had to set the PYTHONPATH to site-packages



    set PYTHONPATH=C:PythonEnvsprojectLibsite-packages


    instead of C:/Python/Envs/project/






    share|improve this answer




























      1














      I had to set the PYTHONPATH to site-packages



      set PYTHONPATH=C:PythonEnvsprojectLibsite-packages


      instead of C:/Python/Envs/project/






      share|improve this answer


























        1












        1








        1







        I had to set the PYTHONPATH to site-packages



        set PYTHONPATH=C:PythonEnvsprojectLibsite-packages


        instead of C:/Python/Envs/project/






        share|improve this answer













        I had to set the PYTHONPATH to site-packages



        set PYTHONPATH=C:PythonEnvsprojectLibsite-packages


        instead of C:/Python/Envs/project/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 10:08









        ipaipa

        1,01811331




        1,01811331
































            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%2f53480120%2fembedding-pybind11-with-virtual-environment%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

            Ottavio Pratesi

            Tricia Helfer

            15 giugno