Skip to content

Commit f81f26f

Browse files
authored
Change colab environment detection logic. (#230)
Now that Kaggle uses the Colab as a base image, the `COLAB_RELEASE_TAG` environment variable is set in the Kaggle image which is not a good signal to detect whether we are running in the Colab environment or not and this has caused the following issue: https://b/399118140. Updated the Colab environment detection logic to rely on the ipython kernel name instead and save that value to avoid having to recompute it each time `is_in_colab_notebook` is called. I also fixed `get_colab_credentials` to ensure it doesn't fail when used in the wrong environment and simply skip (same as if the credentials were not found). This wasn't necessary given this is gated by `is_in_colab_notebook()` but it adds extra safety. BUG=b/399118140
1 parent 842cc18 commit f81f26f

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
## Next Release
44

5+
* Fix Colab environment detection logic ([#230](https://github.com/Kaggle/kagglehub/pull/230))
6+
57
## v0.3.9 (February 18, 2025)
68

7-
* Renamed load_datset to dataset_load.
9+
* Renamed load_datset to dataset_load ([#228](https://github.com/Kaggle/kagglehub/pull/228))
810

911
## v0.3.8 (February 13, 2025)
1012

11-
* Moved signing as optional feature due to dependency issue.
13+
* Moved signing as optional feature due to dependency issue ([#225](https://github.com/Kaggle/kagglehub/pull/225))
1214

1315
## v0.3.7 (January 31, 2025)
1416

src/kagglehub/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ def get_colab_credentials() -> Optional[KaggleApiCredentials]:
163163
# userdata access is already thread-safe after b/318732641
164164
try:
165165
from google.colab import userdata # type: ignore[import]
166-
from google.colab.errors import Error as ColabError # type: ignore[import]
167166
except ImportError:
168167
return None
169168

@@ -172,6 +171,7 @@ def get_colab_credentials() -> Optional[KaggleApiCredentials]:
172171
key = _normalize_whitespace(userdata.get(COLAB_SECRET_KEY))
173172
if username and key:
174173
return KaggleApiCredentials(username=username, key=key)
175-
except ColabError:
176-
pass
177-
return None
174+
return None
175+
except Exception:
176+
# fail to get Kaggle credentials from Colab secrets. Skip...
177+
return None

src/kagglehub/env.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@
1111

1212
logger = logging.getLogger(__name__)
1313

14+
try:
15+
from IPython import get_ipython # type: ignore
16+
17+
# Set to `True` if script is running in a Google Colab notebook.
18+
# Taken from https://stackoverflow.com/a/63519730.
19+
_is_google_colab = "google.colab" in str(get_ipython())
20+
except (NameError, ModuleNotFoundError):
21+
_is_google_colab = False
22+
1423

1524
def is_in_colab_notebook() -> bool:
16-
return os.getenv("COLAB_RELEASE_TAG") is not None
25+
return _is_google_colab
1726

1827

1928
def is_in_kaggle_notebook() -> bool:

tests/test_kaggle_api_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_get_user_agent_kkb(self) -> None:
9696
"COLAB_RELEASE_TAG": "release-colab-20230531-060125-RC00",
9797
},
9898
)
99+
@patch("kagglehub.env._is_google_colab", True)
99100
def test_get_user_agent_colab(self) -> None:
100101
self.assertEqual(
101102
clients.get_user_agent(),

0 commit comments

Comments
 (0)