diff --git a/linopy/__init__.py b/linopy/__init__.py index 7f5acd46..415950eb 100644 --- a/linopy/__init__.py +++ b/linopy/__init__.py @@ -21,7 +21,12 @@ from linopy.model import Model, Variable, Variables, available_solvers from linopy.objective import Objective from linopy.piecewise import breakpoints -from linopy.remote import OetcHandler, RemoteHandler +from linopy.remote import RemoteHandler + +try: + from linopy.remote import OetcCredentials, OetcHandler, OetcSettings # noqa: F401 +except ImportError: + pass __all__ = ( "Constraint", diff --git a/linopy/model.py b/linopy/model.py index 7b8396f4..049093de 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -67,7 +67,12 @@ add_disjunctive_piecewise_constraints, add_piecewise_constraints, ) -from linopy.remote import OetcHandler, RemoteHandler +from linopy.remote import RemoteHandler + +try: + from linopy.remote import OetcHandler +except ImportError: + OetcHandler = None # type: ignore from linopy.solver_capabilities import SolverFeature, solver_supports from linopy.solvers import ( IO_APIS, diff --git a/linopy/remote/__init__.py b/linopy/remote/__init__.py index 0ae1df26..d3d5e162 100644 --- a/linopy/remote/__init__.py +++ b/linopy/remote/__init__.py @@ -8,9 +8,13 @@ - OetcHandler: Cloud-based execution via OET Cloud service """ -from linopy.remote.oetc import OetcCredentials, OetcHandler, OetcSettings from linopy.remote.ssh import RemoteHandler +try: + from linopy.remote.oetc import OetcCredentials, OetcHandler, OetcSettings +except ImportError: + pass + __all__ = [ "RemoteHandler", "OetcHandler", diff --git a/linopy/remote/oetc.py b/linopy/remote/oetc.py index 5bea9c7c..ee94fd43 100644 --- a/linopy/remote/oetc.py +++ b/linopy/remote/oetc.py @@ -9,10 +9,15 @@ from datetime import datetime, timedelta from enum import Enum -import requests -from google.cloud import storage -from google.oauth2 import service_account -from requests import RequestException +try: + import requests + from google.cloud import storage + from google.oauth2 import service_account + from requests import RequestException + + _oetc_deps_available = True +except ImportError: + _oetc_deps_available = False import linopy @@ -85,6 +90,11 @@ class JobResult: class OetcHandler: def __init__(self, settings: OetcSettings) -> None: + if not _oetc_deps_available: + raise ImportError( + "The 'google-cloud-storage' and 'requests' packages are required " + "for OetcHandler. Install them with: pip install linopy[oetc]" + ) self.settings = settings self.jwt = self.__sign_in() self.cloud_provider_credentials = self.__get_cloud_provider_credentials() diff --git a/pyproject.toml b/pyproject.toml index 0f5bd326..aaac2cf1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,8 +37,6 @@ dependencies = [ "tqdm", "deprecation", "packaging", - "google-cloud-storage", - "requests", ] [project.urls] @@ -46,6 +44,10 @@ Homepage = "https://github.com/PyPSA/linopy" Source = "https://github.com/PyPSA/linopy" [project.optional-dependencies] +oetc = [ + "google-cloud-storage", + "requests", +] docs = [ "ipython==8.26.0", "numpydoc==1.7.0", diff --git a/test/remote/test_oetc.py b/test/remote/test_oetc.py index d937e376..0704d24d 100644 --- a/test/remote/test_oetc.py +++ b/test/remote/test_oetc.py @@ -5,10 +5,11 @@ from unittest.mock import Mock, patch import pytest -import requests -from requests import RequestException -from linopy.remote.oetc import ( +requests = pytest.importorskip("requests") +from requests import RequestException # noqa: E402 + +from linopy.remote.oetc import ( # noqa: E402 AuthenticationResult, ComputeProvider, GcpCredentials, diff --git a/test/remote/test_oetc_job_polling.py b/test/remote/test_oetc_job_polling.py index 96ec98b4..4b2681f9 100644 --- a/test/remote/test_oetc_job_polling.py +++ b/test/remote/test_oetc_job_polling.py @@ -9,9 +9,11 @@ from unittest.mock import Mock, patch import pytest -from requests import RequestException -from linopy.remote.oetc import ( +requests = pytest.importorskip("requests") +from requests import RequestException # noqa: E402 + +from linopy.remote.oetc import ( # noqa: E402 AuthenticationResult, ComputeProvider, OetcCredentials,