Skip to content

Commit 7677377

Browse files
committed
SNOW-2012666: Implemented lazy import for pandas in the connector
1 parent c53aad7 commit 7677377

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/snowflake/connector/options.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ def warn_incompatible_dep(
6161
)
6262

6363

64+
def pandas():
65+
try:
66+
return importlib.import_module("pandas")
67+
except ImportError:
68+
return MissingPandas()
69+
70+
71+
def pyarrow():
72+
try:
73+
pyarrow = importlib.import_module("pyarrow")
74+
return pyarrow
75+
except ImportError:
76+
raise errors.MissingDependencyError("pyarrow")
77+
78+
6479
def _import_or_missing_pandas_option() -> (
6580
tuple[ModuleLikeObject, ModuleLikeObject, bool]
6681
):
@@ -114,6 +129,12 @@ def _import_or_missing_pandas_option() -> (
114129
return MissingPandas(), MissingPandas(), False
115130

116131

132+
def installed_pandas() -> bool:
133+
"""This function checks if pandas is available and compatible."""
134+
_, _, installed = _import_or_missing_pandas_option()
135+
return installed
136+
137+
117138
def _import_or_missing_keyring_option() -> tuple[ModuleLikeObject, bool]:
118139
"""This function tries importing the following packages: keyring.
119140
@@ -127,5 +148,22 @@ def _import_or_missing_keyring_option() -> tuple[ModuleLikeObject, bool]:
127148

128149

129150
# Create actual constants to be imported from this file
130-
pandas, pyarrow, installed_pandas = _import_or_missing_pandas_option()
151+
try:
152+
pyarrow = importlib.import_module("pyarrow")
153+
except ImportError:
154+
raise errors.MissingDependencyError("pyarrow")
155+
131156
keyring, installed_keyring = _import_or_missing_keyring_option()
157+
158+
159+
def __getattr__(name):
160+
if name == "pandas":
161+
try:
162+
return importlib.import_module("pandas")
163+
except ImportError:
164+
return MissingPandas()
165+
166+
elif name == "installed_pandas":
167+
return installed_pandas()
168+
169+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

0 commit comments

Comments
 (0)