PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
peridem_env.py
Go to the documentation of this file.
1# -------------------------------------------
2# Copyright (c) 2021 - 2026 Prashant K. Jha
3# -------------------------------------------
4# PeriDEM https://github.com/prashjha/PeriDEM
5#
6# Distributed under the Boost Software License, Version 1.0. (See accompanying
7# file LICENSE)
8
9"""Make ``import peridem`` work from a build tree.
10
11Examples import this first so they run straight from a checkout without
12``pip install``. If peridem is already importable (installed, or PYTHONPATH is
13set) this does nothing.
14"""
15
16from __future__ import annotations
17
18import sys
19from pathlib import Path
20
21__all__ = ["REPO_ROOT", "ensure_peridem"]
22
23REPO_ROOT = next(
24 p for p in Path(__file__).resolve().parents
25 if (p / "python" / "peridem" / "__init__.py").is_file()
26)
27
28_BUILD_DIRS = ("build/linux/python", "build/python", "build/Release/python",
29 "build/Debug/python")
30
31
32def ensure_peridem() -> None:
33 try:
34 import peridem # noqa: F401
35
36 return
37 except ImportError:
38 pass
39 # The staged package under build/ carries the compiled _core; the source
40 # python/ directory does not, so it must never shadow it.
41 for rel in _BUILD_DIRS:
42 cand = REPO_ROOT / rel
43 if (cand / "peridem" / "__init__.py").is_file():
44 sys.path.insert(0, str(cand))
45 break
46 else:
47 sys.path.insert(0, str(REPO_ROOT / "python"))
48 try:
49 import peridem # noqa: F401
50 except ImportError as exc: # pragma: no cover - build guidance
51 raise SystemExit(
52 "cannot import peridem. Build the extension first:\n"
53 f" cmake -S {REPO_ROOT} -B {REPO_ROOT}/build -DEnable_Python=ON\n"
54 f" cmake --build {REPO_ROOT}/build --target peridem_core PeriDEM -j\n"
55 f"({exc})"
56 ) from exc
57
58
None ensure_peridem()