OGS
ogs_embedded_python.cpp
Go to the documentation of this file.
1
11#include "ogs_embedded_python.h"
12
13#include <pybind11/embed.h>
14
15#include <algorithm>
16
17#include "BaseLib/Logging.h"
21
23{
24 DBUG("Binding Python module OpenGeoSys.");
25
29
30 // Check for activated virtual environment and add it to sys.path
31 pybind11::exec(R"(
32 import os
33 import sys
34 if "VIRTUAL_ENV" in os.environ:
35 venv_site_packages_path = f"{os.environ['VIRTUAL_ENV']}/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages"
36 if os.path.exists(venv_site_packages_path):
37 print(
38 f"Virtual environment detected, adding {venv_site_packages_path} to sys.path."
39 )
40 sys.path.insert(0, venv_site_packages_path)
41 )");
42}
43
44namespace ApplicationsLib
45{
46pybind11::scoped_interpreter setupEmbeddedPython()
47{
48 // Allows ogs to be interrupted by SIGINT, which otherwise is handled by
49 // python. See
50 // https://docs.python.org/3/c-api/exceptions.html#c.PyErr_CheckSignals and
51 // https://pybind11.readthedocs.io/en/stable/faq.html#how-can-i-properly-handle-ctrl-c-in-long-running-functions
52 constexpr bool init_signal_handlers = false;
53 return pybind11::scoped_interpreter{init_signal_handlers};
54}
55
56} // namespace ApplicationsLib
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
pybind11::scoped_interpreter setupEmbeddedPython()
void pythonBindSourceTerm(pybind11::module &m)
Creates Python bindings for the Python source term class.
void pythonBindBoundaryCondition(pybind11::module &m)
Creates Python bindings for the Python BC class.
void bheInflowpythonBindBoundaryCondition(pybind11::module &m)
Creates BHE Inflow Python bindings for the Python BC class.
PYBIND11_EMBEDDED_MODULE(OpenGeoSys, m)