OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Guidance for how to build a static linked version of numpy

  • Thread starter Thread starter Luca Faggion
  • Start date Start date
L

Luca Faggion

Guest
I'm writing an application that needs to do some data analisys and I've currently embedded a static linked version of Python into it (so the application can be shipped as a single executable as per requirements) the problem is that in doing so I lose the ability to use Python extension modules such as numpy. I've searched quite a bit online but seems to no avail to find information/guides/infos on how I can build a static linked version of numpy for my static interpreter or a way to include it as a builtin extension into the static library.

heres the code that shows that the libary even if installed cannot be found or loaded

Code:
#include <pybind11/embed.h>
namespace py = pybind11;
using namespace py::literals;

QString MAppContext::test()
{
    py::scoped_interpreter guard{};
    py::dict locals = py::dict("name"_a="World", "number"_a=42);
    py::exec(R"(
        import sys
        message = ""
        try:
            import numpy as np
            message = s.to_string()
        except ModuleNotFoundError as moduleErr:
            message = "[Error]: Failed to import (Module Not Found) {}.".format(moduleErr.args[0])
        except ImportError as impErr:
            message = "[Error]: Failed to import (Import Error) {}.".format(impErr.args[0])
        except Exception as e:
            message = 'An exception occurred: {}'.format(e)
    )", py::globals(), locals);

    auto message = locals["message"].cast<std::string>();
    return QString::fromStdString(message);
}

the message returned is Module not found.
<p>I'm writing an application that needs to do some data analisys and I've currently embedded a static linked version of Python into it (so the application can be shipped as a single executable as per requirements) the problem is that in doing so I lose the ability to use Python extension modules such as numpy. I've searched quite a bit online but seems to no avail to find information/guides/infos on how I can build a static linked version of numpy for my static interpreter or a way to include it as a builtin extension into the static library.</p>
<p>heres the code that shows that the libary even if installed cannot be found or loaded</p>
<pre><code>#include <pybind11/embed.h>
namespace py = pybind11;
using namespace py::literals;

QString MAppContext::test()
{
py::scoped_interpreter guard{};
py::dict locals = py::dict("name"_a="World", "number"_a=42);
py::exec(R"(
import sys
message = ""
try:
import numpy as np
message = s.to_string()
except ModuleNotFoundError as moduleErr:
message = "[Error]: Failed to import (Module Not Found) {}.".format(moduleErr.args[0])
except ImportError as impErr:
message = "[Error]: Failed to import (Import Error) {}.".format(impErr.args[0])
except Exception as e:
message = 'An exception occurred: {}'.format(e)
)", py::globals(), locals);

auto message = locals["message"].cast<std::string>();
return QString::fromStdString(message);
}
</code></pre>
<p>the message returned is Module not found.</p>
 

Latest posts

Top