Skip to content
Snippets Groups Projects
Unverified Commit eaeeaf1c authored by Lars Bilke's avatar Lars Bilke
Browse files

[py] Detect activated venv at runtime in embedded interpreter.

Removed hard-coded build-time venv.
parent 38989b7c
No related branches found
No related tags found
No related merge requests found
...@@ -367,9 +367,6 @@ ProjectData::ProjectData(BaseLib::ConfigTree const& project_config, ...@@ -367,9 +367,6 @@ ProjectData::ProjectData(BaseLib::ConfigTree const& project_config,
// Append to python's module search path // Append to python's module search path
auto py_path = py::module::import("sys").attr("path"); auto py_path = py::module::import("sys").attr("path");
py_path.attr("append")(script_directory); // .prj or -s directory py_path.attr("append")(script_directory); // .prj or -s directory
// virtualenv
py_path.attr("append")(
CMakeInfoLib::CMakeInfo::python_virtualenv_sitepackages);
auto const script_path = auto const script_path =
BaseLib::copyPathToFileName(*python_script, script_directory); BaseLib::copyPathToFileName(*python_script, script_directory);
......
...@@ -10,9 +10,10 @@ ...@@ -10,9 +10,10 @@
#include "ogs_embedded_python.h" #include "ogs_embedded_python.h"
#include <algorithm>
#include <pybind11/embed.h> #include <pybind11/embed.h>
#include <algorithm>
#include "BaseLib/Logging.h" #include "BaseLib/Logging.h"
#include "ProcessLib/BoundaryConditionAndSourceTerm/Python/BHEInflowPythonBoundaryConditionModule.h" #include "ProcessLib/BoundaryConditionAndSourceTerm/Python/BHEInflowPythonBoundaryConditionModule.h"
#include "ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonBoundaryConditionModule.h" #include "ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonBoundaryConditionModule.h"
...@@ -25,6 +26,19 @@ PYBIND11_EMBEDDED_MODULE(OpenGeoSys, m) ...@@ -25,6 +26,19 @@ PYBIND11_EMBEDDED_MODULE(OpenGeoSys, m)
ProcessLib::pythonBindBoundaryCondition(m); ProcessLib::pythonBindBoundaryCondition(m);
ProcessLib::bheInflowpythonBindBoundaryCondition(m); ProcessLib::bheInflowpythonBindBoundaryCondition(m);
ProcessLib::SourceTerms::Python::pythonBindSourceTerm(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 #ifndef OGS_BUILD_SHARED_LIBS
......
...@@ -17,7 +17,6 @@ namespace CMakeInfoLib ...@@ -17,7 +17,6 @@ namespace CMakeInfoLib
namespace CMakeInfo namespace CMakeInfo
{ {
const std::string python_virtualenv_sitepackages("@Python_SITEARCH_NATIVE@");
const std::string cmake_args("@CMAKE_ARGS_ESCAPED@"); const std::string cmake_args("@CMAKE_ARGS_ESCAPED@");
} // namespace CMakeInfo } // namespace CMakeInfo
} // namespace CMakeInfoLib } // namespace CMakeInfoLib
...@@ -21,7 +21,6 @@ namespace CMakeInfoLib ...@@ -21,7 +21,6 @@ namespace CMakeInfoLib
namespace CMakeInfo namespace CMakeInfo
{ {
extern CMAKEINFOLIB_EXPORT const std::string python_virtualenv_sitepackages;
extern CMAKEINFOLIB_EXPORT const std::string cmake_args; extern CMAKEINFOLIB_EXPORT const std::string cmake_args;
} // namespace CMakeInfo } // namespace CMakeInfo
} // namespace CMakeInfoLib } // namespace CMakeInfoLib
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment