File size: 1,155 Bytes
5f38e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Lazy-import helpers for the learning extras.

Each function returns the imported module if available; otherwise raises
``ImportError`` with the exact pip command needed to fix it. This keeps
the scientific core importable on machines without PyTorch / SB3 / Gym.
"""

from __future__ import annotations

from types import ModuleType


def require_torch() -> ModuleType:
    try:
        import torch
    except ImportError as exc:
        raise ImportError(
            "The learning layer needs PyTorch. Install with:  pip install 'cdpr[learn]'"
        ) from exc
    return torch


def require_gymnasium() -> ModuleType:
    try:
        import gymnasium
    except ImportError as exc:
        raise ImportError(
            "The RL environment needs Gymnasium. Install with:  pip install 'cdpr[learn]'"
        ) from exc
    return gymnasium


def require_stable_baselines3() -> ModuleType:
    try:
        import stable_baselines3
    except ImportError as exc:
        raise ImportError(
            "The SB3 wrappers need Stable-Baselines3. "
            "Install with:  pip install 'cdpr[rl]'"
        ) from exc
    return stable_baselines3