Skip to content

Commit

Permalink
Merge branch 'py-venv' into 'master'
Browse files Browse the repository at this point in the history
[py] Detect runtime venv in embedded interpreter

See merge request ogs/ogs!5075
  • Loading branch information
bilke committed Aug 16, 2024
2 parents 84f677e + eaeeaf1 commit e9224df
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
3 changes: 0 additions & 3 deletions Applications/ApplicationsLib/ProjectData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,6 @@ ProjectData::ProjectData(BaseLib::ConfigTree const& project_config,
// Append to python's module search path
auto py_path = py::module::import("sys").attr("path");
py_path.attr("append")(script_directory); // .prj or -s directory
// virtualenv
py_path.attr("append")(
CMakeInfoLib::CMakeInfo::python_virtualenv_sitepackages);

auto const script_path =
BaseLib::copyPathToFileName(*python_script, script_directory);
Expand Down
16 changes: 15 additions & 1 deletion Applications/CLI/ogs_embedded_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

#include "ogs_embedded_python.h"

#include <algorithm>
#include <pybind11/embed.h>

#include <algorithm>

#include "BaseLib/Logging.h"
#include "ProcessLib/BoundaryConditionAndSourceTerm/Python/BHEInflowPythonBoundaryConditionModule.h"
#include "ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonBoundaryConditionModule.h"
Expand All @@ -25,6 +26,19 @@ PYBIND11_EMBEDDED_MODULE(OpenGeoSys, m)
ProcessLib::pythonBindBoundaryCondition(m);
ProcessLib::bheInflowpythonBindBoundaryCondition(m);
ProcessLib::SourceTerms::Python::pythonBindSourceTerm(m);

// Check for activated virtual environment and add it to sys.path
pybind11::exec(R"(
import os
import sys
if "VIRTUAL_ENV" in os.environ:
venv_site_packages_path = f"{os.environ['VIRTUAL_ENV']}/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages"
if os.path.exists(venv_site_packages_path):
print(
f"Virtual environment detected, adding {venv_site_packages_path} to sys.path."
)
sys.path.insert(0, venv_site_packages_path)
)");
}

#ifndef OGS_BUILD_SHARED_LIBS
Expand Down
8 changes: 5 additions & 3 deletions Applications/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ endif()
add_subdirectory(ogs)
add_subdirectory(ogs.simulator)
add_subdirectory(ogs.mesh)
add_subdirectory(ogs.callbacks)
if(OGS_BUILD_WHEEL)
add_subdirectory(ogs.callbacks)
endif()

if(OGS_USE_PIP)
set_target_properties(
simulator mesh callbacks PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${_py_build_location}
simulator mesh PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${_py_build_location}
)
file(
COPY ogs/.
Expand Down
12 changes: 11 additions & 1 deletion Applications/Python/ogs.callbacks/ogs_callbacks_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
)");
}
1 change: 0 additions & 1 deletion InfoLib/CMakeInfo.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace CMakeInfoLib

namespace CMakeInfo
{
const std::string python_virtualenv_sitepackages("@Python_SITEARCH_NATIVE@");
const std::string cmake_args("@CMAKE_ARGS_ESCAPED@");
} // namespace CMakeInfo
} // namespace CMakeInfoLib
1 change: 0 additions & 1 deletion InfoLib/CMakeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace CMakeInfoLib

namespace CMakeInfo
{
extern CMAKEINFOLIB_EXPORT const std::string python_virtualenv_sitepackages;
extern CMAKEINFOLIB_EXPORT const std::string cmake_args;
} // namespace CMakeInfo
} // namespace CMakeInfoLib

0 comments on commit e9224df

Please sign in to comment.