From 38989b7c8a09812c47c35277c7c9e5d058d3dbc8 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Thu, 15 Aug 2024 14:56:54 +0200
Subject: [PATCH] [py] Added error message when trying to import ogs.callbacks
 from the embedded interpreter.

This can happen if you have the ogs-wheel installed but are running a local ogs binary. In this case use `import OpenGeoSys` or uninstall the wheel.
---
 .../Python/ogs.callbacks/ogs_callbacks_module.cpp    | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/Applications/Python/ogs.callbacks/ogs_callbacks_module.cpp b/Applications/Python/ogs.callbacks/ogs_callbacks_module.cpp
index c2e0789138c..45b1bfedbef 100644
--- a/Applications/Python/ogs.callbacks/ogs_callbacks_module.cpp
+++ b/Applications/Python/ogs.callbacks/ogs_callbacks_module.cpp
@@ -8,9 +8,11 @@
  *
  */
 
-#include <algorithm>
+#include <pybind11/eval.h>
 #include <pybind11/pybind11.h>
 
+#include <algorithm>
+
 #include "ProcessLib/BoundaryConditionAndSourceTerm/Python/BHEInflowPythonBoundaryConditionModule.h"
 #include "ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonBoundaryConditionModule.h"
 #include "ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonSourceTermModule.h"
@@ -21,4 +23,12 @@ PYBIND11_MODULE(callbacks, m)
     ProcessLib::pythonBindBoundaryCondition(m);
     ProcessLib::bheInflowpythonBindBoundaryCondition(m);
     ProcessLib::SourceTerms::Python::pythonBindSourceTerm(m);
+
+    pybind11::exec(R"(
+        try:
+            import OpenGeoSys
+            raise ImportError("The Python interpreter seems to be running inside the OGS binary, but you are about to import a Python module from OGS's Python bindings. Please do not import ogs.callbacks, but use the OpenGeoSys module, instead.")
+        except ModuleNotFoundError:
+            pass
+    )");
 }
-- 
GitLab