0

I'm trying to read a string list output from a Python script, in order to pass it to a JUCE MainComponent (written in C++).

The code is the following (which is a starting one, just to test the pybind library):

#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
namespace py = pybind11;

MainComponent::MainComponent()
{
    auto math = py::module::import("math");
    auto resultobj = math.attr("sqrt")(2);
    double result = resultobj.cast<double>();
}

However, I always encounter the following error:

_PyRuntime.gc.**generation0** was nullptr.

Any suggestions? Thanks in advance

1 Answer 1

1

You have to first initialize the Python interpreter, like this:

MainComponent::MainComponent()
{
    py::scoped_interpreter guard{};

    auto math = py::module::import("math");
    auto resultobj = math.attr("sqrt")(2);
    double result = resultobj.cast<double>();
}

You also have to link with the needed python libraries. In CMake, this means adding

target_link_libraries(your_target PRIVATE pybind11::embed)

to your CMakeLists.txt file.

Sign up to request clarification or add additional context in comments.

3 Comments

I have already added py::scoped_interpreter guard{}; but the error is still present.
Moreover, the JUCE framework did not generate the CMakeLists.txt file. Maybe I wasn't able to retrive this file... do you know where it is located inside a JUCE project folder?
I'm not sure how JUCE framework's build system works, but it seems to have CMake support (forum.juce.com/t/native-built-in-cmake-support-in-juce/38700)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.