diff --git a/openmc/__init__.py b/openmc/__init__.py index 9c95c4f0ea2..2b803e7cb63 100644 --- a/openmc/__init__.py +++ b/openmc/__init__.py @@ -58,6 +58,16 @@ def get_core_path(subdir, pattern="*", recursive=False): list: A list of matched paths. """ path = os.path.join(__path__[0], "core", subdir) + if not os.path.exists(path): + import sysconfig + path = os.path.join(sysconfig.get_path("platlib"), "openmc", "core", subdir) + warnings.warn( + "It seems OpenMC is being run from its source directory. " + "This setup is not recommended as it may lead to unexpected behavior, " + "such as conflicts between source and installed versions. " + "Please run your script from outside the OpenMC source tree.", + RuntimeWarning + ) search_pattern = os.path.join(path, "**", pattern) if recursive else os.path.join(path, pattern) return glob.glob(search_pattern, recursive=recursive) if os.path.exists(path) else [] diff --git a/openmc/lib/__init__.py b/openmc/lib/__init__.py index f9d4a57d7a5..cd49665a5f3 100644 --- a/openmc/lib/__init__.py +++ b/openmc/lib/__init__.py @@ -14,37 +14,19 @@ from ctypes import CDLL, c_bool, c_int import os -import warnings -def load_openmc_library(): - try: - # Attempt to load the library from OpenMC - import openmc - _filename = openmc.lib[0] - if os.path.isfile(_filename): - return CDLL(str(_filename)) - raise FileNotFoundError - except (IndexError, FileNotFoundError): - # Attempt to load the library from the installed module - import importlib - openmc = importlib.import_module("openmc") - _filename = openmc.lib[0] - if os.path.isfile(_filename): - warnings.warn( - "It seems OpenMC is being run from its source directory. " - "This setup is not recommended as it may lead to unexpected behavior, " - "such as conflicts between source and installed versions. " - "Please run your script from outside the OpenMC source tree.", - RuntimeWarning - ) - return CDLL(str(_filename)) - raise RuntimeError( - "Unable to load the OpenMC library. Please ensure OpenMC " - "is installed correctly and accessible from your Python environment." - ) - -# Load the OpenMC shared library -_dll = load_openmc_library() +if os.environ.get('READTHEDOCS', None) != 'True': + # Open shared library + import openmc + _filename = openmc.lib[0] + _dll = CDLL(str(_filename)) # TODO: Remove str() when Python 3.12+ +else: + # For documentation builds, we don't actually have the shared library + # available. Instead, we create a mock object so that when the modules + # within the openmc.lib package try to configure arguments and return + # values for symbols, no errors occur + from unittest.mock import Mock + _dll = Mock() def _dagmc_enabled():