From b35c1436e3026ab100d355232fea46da97262278 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 19 Dec 2023 13:02:48 +0100
Subject: [PATCH] [py] Import OpenGeoSys module properly.

Needs to be imported with:

try:
    import ogs.callbacks as OpenGeoSys
except ModuleNotFoundError:
    import OpenGeoSys

`ogs.callbacks` is needed when imported via pip installed package.
`import OpenGeoSys` else.
---
 .../bcs_laplace_eq.py                                  |  5 ++++-
 .../sin_x_sin_y_source_term.py                         |  5 ++++-
 .../IdealGas/flow_pressure_boundary/python_boundary.py |  5 ++++-
 .../Unconfined_Compression_early/python_boundary.py    |  5 ++++-
 .../InjectionProduction1D/python_boundary.py           |  5 ++++-
 .../InjectionProduction1D/python_boundary_staggered.py |  5 ++++-
 .../Linear/PythonHertzContact/hertz_contact_bc.py      |  5 ++++-
 Tests/Data/Mechanics/Linear/PythonPiston/piston_bc.py  |  5 ++++-
 .../ComponentTransport/elder/elder-python-bcs.py       |  5 ++++-
 Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy.py     |  6 +++++-
 .../Parabolic/T/3D_3BHEs_array/bcs_tespy_closedloop.py |  6 +++++-
 .../bcs_tespy_and_serverCommunication.py               |  6 +++++-
 .../simulationX_test.py                                |  5 ++++-
 .../docs/benchmarks/elliptic/poisson_equation/index.md |  6 +++++-
 web/content/docs/userguide/features/python_bc.md       | 10 ++++++++--
 15 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/Tests/Data/Elliptic/square_1x1_SteadyStateDiffusion_Python/bcs_laplace_eq.py b/Tests/Data/Elliptic/square_1x1_SteadyStateDiffusion_Python/bcs_laplace_eq.py
index ca7c2f7211c..9dbe9aac181 100644
--- a/Tests/Data/Elliptic/square_1x1_SteadyStateDiffusion_Python/bcs_laplace_eq.py
+++ b/Tests/Data/Elliptic/square_1x1_SteadyStateDiffusion_Python/bcs_laplace_eq.py
@@ -1,6 +1,9 @@
 from math import cos, cosh, pi, sin, sinh
 
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 a = 2.0 * pi / 3.0
 
diff --git a/Tests/Data/Elliptic/square_1x1_SteadyStateDiffusion_Python/sin_x_sin_y_source_term.py b/Tests/Data/Elliptic/square_1x1_SteadyStateDiffusion_Python/sin_x_sin_y_source_term.py
index e441394b693..c8b05010782 100644
--- a/Tests/Data/Elliptic/square_1x1_SteadyStateDiffusion_Python/sin_x_sin_y_source_term.py
+++ b/Tests/Data/Elliptic/square_1x1_SteadyStateDiffusion_Python/sin_x_sin_y_source_term.py
@@ -1,6 +1,9 @@
 from math import pi, sin
 
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 a = 2.0 * pi
 b = 2.0 * pi
diff --git a/Tests/Data/HydroMechanics/IdealGas/flow_pressure_boundary/python_boundary.py b/Tests/Data/HydroMechanics/IdealGas/flow_pressure_boundary/python_boundary.py
index dbff37df9ce..7889b8fd670 100644
--- a/Tests/Data/HydroMechanics/IdealGas/flow_pressure_boundary/python_boundary.py
+++ b/Tests/Data/HydroMechanics/IdealGas/flow_pressure_boundary/python_boundary.py
@@ -1,4 +1,7 @@
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 p_flux_in = 1e-2
 p_0 = 1e5
diff --git a/Tests/Data/HydroMechanics/Linear/Unconfined_Compression_early/python_boundary.py b/Tests/Data/HydroMechanics/Linear/Unconfined_Compression_early/python_boundary.py
index 8a484407730..b676a7a79df 100644
--- a/Tests/Data/HydroMechanics/Linear/Unconfined_Compression_early/python_boundary.py
+++ b/Tests/Data/HydroMechanics/Linear/Unconfined_Compression_early/python_boundary.py
@@ -1,4 +1,7 @@
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 dirichlet_displacement_top = -0.05
 dirichlet_displacement_0 = 0
diff --git a/Tests/Data/HydroMechanics/StaggeredScheme/InjectionProduction1D/python_boundary.py b/Tests/Data/HydroMechanics/StaggeredScheme/InjectionProduction1D/python_boundary.py
index 10a393606fa..703478d3f35 100644
--- a/Tests/Data/HydroMechanics/StaggeredScheme/InjectionProduction1D/python_boundary.py
+++ b/Tests/Data/HydroMechanics/StaggeredScheme/InjectionProduction1D/python_boundary.py
@@ -1,4 +1,7 @@
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 dirichlet_displacement_0 = 0
 neumann_displacement_overburden = -2.125e6
diff --git a/Tests/Data/HydroMechanics/StaggeredScheme/InjectionProduction1D/python_boundary_staggered.py b/Tests/Data/HydroMechanics/StaggeredScheme/InjectionProduction1D/python_boundary_staggered.py
index b4740b7686c..bc9f209eeb8 100644
--- a/Tests/Data/HydroMechanics/StaggeredScheme/InjectionProduction1D/python_boundary_staggered.py
+++ b/Tests/Data/HydroMechanics/StaggeredScheme/InjectionProduction1D/python_boundary_staggered.py
@@ -1,4 +1,7 @@
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 dirichlet_displacement_0 = 0
 neumann_displacement_overburden = -2.125e6
diff --git a/Tests/Data/Mechanics/Linear/PythonHertzContact/hertz_contact_bc.py b/Tests/Data/Mechanics/Linear/PythonHertzContact/hertz_contact_bc.py
index 6dddb584465..36583ee1a82 100644
--- a/Tests/Data/Mechanics/Linear/PythonHertzContact/hertz_contact_bc.py
+++ b/Tests/Data/Mechanics/Linear/PythonHertzContact/hertz_contact_bc.py
@@ -1,4 +1,7 @@
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 SPHERE_RADIUS = 1.0
 START_TIME = 0.0
diff --git a/Tests/Data/Mechanics/Linear/PythonPiston/piston_bc.py b/Tests/Data/Mechanics/Linear/PythonPiston/piston_bc.py
index b7437e06bc5..66d9f1dbcde 100644
--- a/Tests/Data/Mechanics/Linear/PythonPiston/piston_bc.py
+++ b/Tests/Data/Mechanics/Linear/PythonPiston/piston_bc.py
@@ -1,4 +1,7 @@
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 from chamber import *
 
 
diff --git a/Tests/Data/Parabolic/ComponentTransport/elder/elder-python-bcs.py b/Tests/Data/Parabolic/ComponentTransport/elder/elder-python-bcs.py
index 3d815d21b7b..487aa541993 100644
--- a/Tests/Data/Parabolic/ComponentTransport/elder/elder-python-bcs.py
+++ b/Tests/Data/Parabolic/ComponentTransport/elder/elder-python-bcs.py
@@ -1,4 +1,7 @@
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 
 class BCPressure(OpenGeoSys.BoundaryCondition):
diff --git a/Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy.py b/Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy.py
index f9af119c09d..e0b87b5a866 100644
--- a/Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy.py
+++ b/Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy.py
@@ -11,7 +11,11 @@ print(sys.version)
 import os
 
 import numpy as np
-import OpenGeoSys
+
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 from pandas import read_csv
 from tespy.networks import load_network
 
diff --git a/Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy_closedloop.py b/Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy_closedloop.py
index 9af36c6c511..3d41bf42d46 100644
--- a/Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy_closedloop.py
+++ b/Tests/Data/Parabolic/T/3D_3BHEs_array/bcs_tespy_closedloop.py
@@ -11,7 +11,11 @@ print(sys.version)
 import os
 
 import numpy as np
-import OpenGeoSys
+
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 from pandas import read_csv
 from tespy.networks import load_network
 
diff --git a/Tests/Data/Parabolic/T/3D_3BHEs_array_python_interface/bcs_tespy_and_serverCommunication.py b/Tests/Data/Parabolic/T/3D_3BHEs_array_python_interface/bcs_tespy_and_serverCommunication.py
index 520254b3b12..6591d9fd595 100644
--- a/Tests/Data/Parabolic/T/3D_3BHEs_array_python_interface/bcs_tespy_and_serverCommunication.py
+++ b/Tests/Data/Parabolic/T/3D_3BHEs_array_python_interface/bcs_tespy_and_serverCommunication.py
@@ -11,7 +11,11 @@ print(sys.version)
 import os
 
 import numpy as np
-import OpenGeoSys
+
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 from pandas import read_csv
 from tespy.networks import load_network
 
diff --git a/Tests/Data/Parabolic/T/3D_Beier_sandbox_python_interface/simulationX_test.py b/Tests/Data/Parabolic/T/3D_Beier_sandbox_python_interface/simulationX_test.py
index c366bd6346b..2656742335b 100644
--- a/Tests/Data/Parabolic/T/3D_Beier_sandbox_python_interface/simulationX_test.py
+++ b/Tests/Data/Parabolic/T/3D_Beier_sandbox_python_interface/simulationX_test.py
@@ -8,7 +8,10 @@
 import sys
 
 print(sys.version)
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 from pandas import read_csv
 
 df_server = read_csv(
diff --git a/web/content/docs/benchmarks/elliptic/poisson_equation/index.md b/web/content/docs/benchmarks/elliptic/poisson_equation/index.md
index 7d1542c0bca..29ae58f81d3 100644
--- a/web/content/docs/benchmarks/elliptic/poisson_equation/index.md
+++ b/web/content/docs/benchmarks/elliptic/poisson_equation/index.md
@@ -105,7 +105,11 @@ Python object `sinx_sinx_source_term` that is created in the last line of the
 Python script:
 
 ```python
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
+
 from math import pi, sin
 
 a = 2.0*pi
diff --git a/web/content/docs/userguide/features/python_bc.md b/web/content/docs/userguide/features/python_bc.md
index 86b1ff598df..49beca78657 100644
--- a/web/content/docs/userguide/features/python_bc.md
+++ b/web/content/docs/userguide/features/python_bc.md
@@ -48,7 +48,10 @@ The path to the file can be defined in relative or absolute terms. For example:
 First, the OpenGeoSys module has to be imported:
 
 ```python
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 ```
 
 This module doesn't need to be installed, if OpenGeoSys is compiled with Python support, then it is part of the Python
@@ -127,7 +130,10 @@ Examples of the application of Python boundary conditions can be found in [this
 ## Full example of a Python boundary condition
 
 ```python
-import OpenGeoSys
+try:
+    import ogs.callbacks as OpenGeoSys
+except ModuleNotFoundError:
+    import OpenGeoSys
 
 class BoundaryCondition(OpenGeoSys):
 
-- 
GitLab