0

I have the following code in the Qt Quick Application together with Boost. In this Cpp there is a personal module created using BOOST_PYTHON_MODULE(hello). The main goal is to be able to import hello in Python and call the methods of hello struct. My Python script only contains very simple structure as i just want to see no errors when importing hello.

import hello
print("Import was successful!")

Most of the codes below are copied from a different question in stackoverflow but not entirely so i had to repost the parts.

Main.cpp

#include <cstdlib>  // setenv, atoi
#include <iostream> // cerr, cout, endl
#include <boost/python.hpp>

#include <QGuiApplication>
#include <QQmlApplicationEngine>


struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet()       { return msg;      }
    std::string msg;
};

//---------------------------------------------------------------------------------------------------------------------
/// Staticly linking a Python extension for embedded Python.
BOOST_PYTHON_MODULE(hello)
{
    namespace python = boost::python;
    python::class_<World>("World")
            .def("greet", &World::greet)
            .def("set", &World::set)
            ;
}

//---------------------------------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    namespace python = boost::python;
    try
    {
        int uploaded = PyImport_AppendInittab("hello", &PyInit_hello);
        //This executes else part
        if(uploaded == -1)
          std::cout<< "Module table was not extended: " << uploaded << std::endl;
        else
          std::cout<< "Module Table was extended" << std::endl;

        Py_Initialize();

    } catch (...)
    {
        PyErr_Print();
        return 1;
    }

    return app.exec();
}

Finally, I run my QT application and the return app.exec(); keeps it running while i try and run my python script as mentioned above from the terminal. The python script is in the same directory as the currently running application, not sure if that makes any difference.

Then the error i get is:

Traceback (most recent call last):
  File "test_hilton.py", line 1, in <module>
    import hello
ModuleNotFoundError: No module named 'hello'

Not sure what i am missing here. According to the Python API:

PyImport_AppendInittab - Add a single module to the existing table of built-in modules. This is a convenience wrapper around PyImport_ExtendInittab(), returning -1 if the table could not be extended. The new module can be imported by the name name, and uses the function initfunc as the initialization function called on the first attempted import.

And the If-else part inside the try-catch block inside the main proves that the hello module is being added to the table. Out of ideas on what to do, looked in different places. But still stuck with this part of the problem.

1 Answer 1

0

Since the hello module is defined in that Qt program it is available only in that program. Executing the program doesn't make it available to python interpreter that expects to find hello.py or hello.so (the file extension may vary depending on the operating system) when importing hello by import hello.

You need to build a python module, answer might help.

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

1 Comment

I figured that out after some hours. I realized that my communication should be In real time. Also adding python inside C++ would be impossible in my case. Then after some time i realized i should have just used IPC. Now i am using: ZeroMQ which has worked wonderfully! Thanks for confirming.

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.