Skip to content

Commit 0a40d2c

Browse files
fix: make google-cloud-storage and requests optional dependencies (#589)
Co-authored-by: Fabian Hofmann <fab.hof@gmx.de>
1 parent 5d71b5d commit 0a40d2c

File tree

7 files changed

+43
-14
lines changed

7 files changed

+43
-14
lines changed

linopy/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
from linopy.model import Model, Variable, Variables, available_solvers
2222
from linopy.objective import Objective
2323
from linopy.piecewise import breakpoints
24-
from linopy.remote import OetcHandler, RemoteHandler
24+
from linopy.remote import RemoteHandler
25+
26+
try:
27+
from linopy.remote import OetcCredentials, OetcHandler, OetcSettings # noqa: F401
28+
except ImportError:
29+
pass
2530

2631
__all__ = (
2732
"Constraint",

linopy/model.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@
6767
add_disjunctive_piecewise_constraints,
6868
add_piecewise_constraints,
6969
)
70-
from linopy.remote import OetcHandler, RemoteHandler
70+
from linopy.remote import RemoteHandler
71+
72+
try:
73+
from linopy.remote import OetcHandler
74+
except ImportError:
75+
OetcHandler = None # type: ignore
7176
from linopy.solver_capabilities import SolverFeature, solver_supports
7277
from linopy.solvers import (
7378
IO_APIS,

linopy/remote/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
- OetcHandler: Cloud-based execution via OET Cloud service
99
"""
1010

11-
from linopy.remote.oetc import OetcCredentials, OetcHandler, OetcSettings
1211
from linopy.remote.ssh import RemoteHandler
1312

13+
try:
14+
from linopy.remote.oetc import OetcCredentials, OetcHandler, OetcSettings
15+
except ImportError:
16+
pass
17+
1418
__all__ = [
1519
"RemoteHandler",
1620
"OetcHandler",

linopy/remote/oetc.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
from datetime import datetime, timedelta
1010
from enum import Enum
1111

12-
import requests
13-
from google.cloud import storage
14-
from google.oauth2 import service_account
15-
from requests import RequestException
12+
try:
13+
import requests
14+
from google.cloud import storage
15+
from google.oauth2 import service_account
16+
from requests import RequestException
17+
18+
_oetc_deps_available = True
19+
except ImportError:
20+
_oetc_deps_available = False
1621

1722
import linopy
1823

@@ -85,6 +90,11 @@ class JobResult:
8590

8691
class OetcHandler:
8792
def __init__(self, settings: OetcSettings) -> None:
93+
if not _oetc_deps_available:
94+
raise ImportError(
95+
"The 'google-cloud-storage' and 'requests' packages are required "
96+
"for OetcHandler. Install them with: pip install linopy[oetc]"
97+
)
8898
self.settings = settings
8999
self.jwt = self.__sign_in()
90100
self.cloud_provider_credentials = self.__get_cloud_provider_credentials()

pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ dependencies = [
3737
"tqdm",
3838
"deprecation",
3939
"packaging",
40-
"google-cloud-storage",
41-
"requests",
4240
]
4341

4442
[project.urls]
4543
Homepage = "https://github.com/PyPSA/linopy"
4644
Source = "https://github.com/PyPSA/linopy"
4745

4846
[project.optional-dependencies]
47+
oetc = [
48+
"google-cloud-storage",
49+
"requests",
50+
]
4951
docs = [
5052
"ipython==8.26.0",
5153
"numpydoc==1.7.0",

test/remote/test_oetc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
from unittest.mock import Mock, patch
66

77
import pytest
8-
import requests
9-
from requests import RequestException
108

11-
from linopy.remote.oetc import (
9+
requests = pytest.importorskip("requests")
10+
from requests import RequestException # noqa: E402
11+
12+
from linopy.remote.oetc import ( # noqa: E402
1213
AuthenticationResult,
1314
ComputeProvider,
1415
GcpCredentials,

test/remote/test_oetc_job_polling.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
from unittest.mock import Mock, patch
1010

1111
import pytest
12-
from requests import RequestException
1312

14-
from linopy.remote.oetc import (
13+
requests = pytest.importorskip("requests")
14+
from requests import RequestException # noqa: E402
15+
16+
from linopy.remote.oetc import ( # noqa: E402
1517
AuthenticationResult,
1618
ComputeProvider,
1719
OetcCredentials,

0 commit comments

Comments
 (0)