Allow install_python_stack to run on Colab (#4633)

* Allow install_python_stack to run on Colab

The _COLAB_NO_VENV flag was setting _SKIP_PYTHON_DEPS=true, which
skipped both the PyPI version check (needs $VENV_DIR/bin/python) and
install_python_stack (uses sys.executable, works without a venv).

Introduce a separate _SKIP_VERSION_CHECK flag for the version check,
so install_python_stack still runs on Colab. The _SKIP_PYTHON_DEPS
flag remains available for the "versions match" fast path.

* Remove colab.py workarounds that broke transformers/hf-hub compatibility

PR #4601 added _pip_install_backend_deps(), _bootstrap_studio_venv(),
and _is_colab() to colab.py as workarounds for install_python_stack
being skipped on Colab. These workarounds:
- Stripped version constraints from studio.txt and installed into system Python
- Upgraded huggingface-hub to >=1.0, breaking Colab's pre-installed
  transformers which requires huggingface-hub<1.0

With install_python_stack now running on Colab (previous commit), these
workarounds are unnecessary — all deps are properly installed by setup.sh.
Restore colab.py to its original PR #4237 structure: just get_colab_url(),
show_link(), and start().

* Remove --local flag from setup.sh in Colab notebook

The --local flag is not needed for the standard Colab flow since
install_python_stack now runs on Colab and installs deps from PyPI.
This commit is contained in:
Roland Tannous 2026-03-27 00:29:27 +04:00 committed by GitHub
parent 71781272dd
commit e79a178200
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 6 additions and 88 deletions

View file

@ -64,7 +64,7 @@
"id": "27e68f91"
},
"outputs": [],
"source": "!git clone --depth 1 --branch main https://github.com/unslothai/unsloth.git\n%cd /content/unsloth\n!chmod +x studio/setup.sh && ./studio/setup.sh --local"
"source": "!git clone --depth 1 --branch main https://github.com/unslothai/unsloth.git\n%cd /content/unsloth\n!chmod +x studio/setup.sh && ./studio/setup.sh"
},
{
"cell_type": "markdown",

View file

@ -18,90 +18,6 @@ if _backend_dir not in sys.path:
import _platform_compat # noqa: F401
def _is_colab() -> bool:
"""Detect Google Colab by checking for COLAB_ prefixed env vars."""
import os
return any(k.startswith("COLAB_") for k in os.environ)
def _pip_install_backend_deps() -> None:
"""Install Studio backend dependencies directly into the current Python.
Used on Colab when the Studio venv does not exist (install.sh was not
run). Reads the requirements from studio.txt next to this file.
Version constraints are stripped entirely so pip keeps whatever Colab
already has installed (e.g. huggingface-hub, datasets, transformers)
and only installs genuinely missing packages like structlog, fastapi.
"""
import re
import subprocess
req_file = Path(__file__).parent / "requirements" / "studio.txt"
if not req_file.exists():
return
packages = []
for line in req_file.read_text().splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
# Strip all version constraints -- just keep the package name
pkg_name = re.split(r"[><=!~;\[]", line)[0].strip()
if pkg_name:
packages.append(pkg_name)
if not packages:
return
print("Installing Studio backend dependencies ...")
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "-q"] + packages,
)
# Colab ships huggingface-hub 0.36.x which removed is_offline_mode,
# breaking transformers. Upgrade to 1.0+ which restored it.
try:
from huggingface_hub import is_offline_mode # noqa: F401
except ImportError:
print("Upgrading huggingface-hub (is_offline_mode missing) ...")
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "-q", "huggingface-hub>=1.0"],
)
def _bootstrap_studio_venv() -> None:
"""Expose the Studio venv's site-packages to the current interpreter.
On Colab, notebook cells run outside the venv subshell. Instead of
installing the full stack into system Python, we prepend the venv's
site-packages so that packages like structlog, fastapi, etc. are
importable from notebook cells and take priority over system copies.
If the venv does not exist and we are running on Colab, fall back to
pip-installing the backend dependencies into the current environment
so that imports like structlog and fastapi succeed.
"""
venv_lib = Path.home() / ".unsloth" / "studio" / "unsloth_studio" / "lib"
if not venv_lib.exists():
if _is_colab():
_pip_install_backend_deps()
return
import warnings
warnings.warn(
f"Studio venv not found at {venv_lib.parent} -- run 'unsloth studio setup' first",
stacklevel = 2,
)
return
for sp in venv_lib.glob("python*/site-packages"):
sp_str = str(sp)
if sp_str not in sys.path:
sys.path.insert(0, sp_str)
_bootstrap_studio_venv()
from loggers import get_logger
logger = get_logger(__name__)

View file

@ -346,13 +346,15 @@ fi
# ── Check if Python deps need updating ──
# Compare installed package version against PyPI latest.
# Skip all Python dependency work if versions match (fast update path).
# On Colab (no venv), skip the entire venv-dependent Python deps section.
# On Colab (no venv), skip this version check (it needs $VENV_DIR/bin/python)
# but still run install_python_stack below (it uses sys.executable).
_SKIP_PYTHON_DEPS=false
_SKIP_VERSION_CHECK=false
if [ "$_COLAB_NO_VENV" = true ]; then
_SKIP_PYTHON_DEPS=true
_SKIP_VERSION_CHECK=true
fi
_PKG_NAME="${STUDIO_PACKAGE_NAME:-unsloth}"
if [ "$_SKIP_PYTHON_DEPS" != true ] && [ "${SKIP_STUDIO_BASE:-0}" != "1" ] && [ "${STUDIO_LOCAL_INSTALL:-0}" != "1" ]; then
if [ "$_SKIP_VERSION_CHECK" != true ] && [ "${SKIP_STUDIO_BASE:-0}" != "1" ] && [ "${STUDIO_LOCAL_INSTALL:-0}" != "1" ]; then
# Only check when NOT called from install.sh (which just installed the package)
INSTALLED_VER=$("$VENV_DIR/bin/python" -c "
from importlib.metadata import version