Skip to content

Studio: fix setup Python venv detection#6044

Draft
Imagineer99 wants to merge 1 commit into
unslothai:mainfrom
Imagineer99:fix/studio-python-venv-detection
Draft

Studio: fix setup Python venv detection#6044
Imagineer99 wants to merge 1 commit into
unslothai:mainfrom
Imagineer99:fix/studio-python-venv-detection

Conversation

@Imagineer99
Copy link
Copy Markdown
Collaborator

@Imagineer99 Imagineer99 commented Jun 6, 2026

Summary

Fixes #6017

setup.ps1 could fail Python detection when an incompatible Python, such as 3.14, appeared earlier on PATH. This makes setup use the known Studio venv Python first when available, avoiding false prerequisite failures.

  • Prefer the Studio virtualenv Python created by install.ps1 before falling back to py.exe
  • Add helper logic to validate compatible Python versions >=3.11,<3.14
  • Add the resolved compatible Python directory to the current process PATH

@Imagineer99 Imagineer99 changed the title Studio: Fix setup Python venv detection Studio: fix setup Python venv detection Jun 6, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the studio/setup.ps1 script to prioritize the Python executable from the Studio virtual environment before falling back to py.exe. It introduces helper functions for checking compatible Python versions and updating the process PATH. The review feedback highlights a critical bug in the tilde expansion logic that can cause a crash in PowerShell 5.1 when the home directory is exactly "~", suggesting centralizing this logic into a helper function. Additionally, it recommends tightening the Python version regex with a word boundary to avoid false matches on future Python minor versions like 3.110.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread studio/setup.ps1
Comment on lines +1452 to +1462
$_prereqStudioHome = $null
if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) {
$_prereqStudioHome = $env:UNSLOTH_STUDIO_HOME.Trim()
} elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) {
$_prereqStudioHome = $env:STUDIO_HOME.Trim()
} else {
$_prereqStudioHome = Join-Path $env:USERPROFILE ".unsloth\studio"
}
if ($_prereqStudioHome -eq "~" -or $_prereqStudioHome -like "~/*" -or $_prereqStudioHome -like "~\*") {
$_prereqStudioHome = (Join-Path $env:USERPROFILE $_prereqStudioHome.Substring(1).TrimStart('/','\'))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Resolving the Studio Home directory is a recurring and complex logical check that is duplicated in multiple places in this script (here and at line 1813). Centralizing this logic into a single helper function improves maintainability and ensures consistency.

Additionally, there is a critical bug in the current tilde expansion logic: if $_prereqStudioHome is exactly "~", then $_prereqStudioHome.Substring(1) returns an empty string "". Passing an empty string as the ChildPath argument to Join-Path throws a terminating error in PowerShell 5.1: Join-Path : Cannot bind argument to parameter 'ChildPath' because it is an empty string. Since $ErrorActionPreference = "Stop" is set, this will crash the setup script.

Defining a helper function Get-StudioHome that handles the exact "~" case separately resolves both the duplication and the potential crash.

function Get-StudioHome {
    $homeDir = $null
    if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) {
        $homeDir = $env:UNSLOTH_STUDIO_HOME.Trim()
    } elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) {
        $homeDir = $env:STUDIO_HOME.Trim()
    } else {
        $homeDir = Join-Path $env:USERPROFILE ".unsloth\studio"
    }
    if ($homeDir -eq "~") {
        $homeDir = $env:USERPROFILE
    } elseif ($homeDir -like "~/*" -or $homeDir -like "~\*") {
        $homeDir = Join-Path $env:USERPROFILE $homeDir.Substring(1).TrimStart('/','\')
    }
    return $homeDir
}

$_prereqStudioHome = Get-StudioHome
References
  1. Centralize recurring or complex logical checks into a single helper function and reuse it across the codebase to ensure consistency and simplify maintenance.

Comment thread studio/setup.ps1
param([string]$PythonExe)
try {
$out = & $PythonExe --version 2>&1 | Out-String
if ($out -match 'Python (3\.(11|12|13)(\.\d+)?)') {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The regular expression 'Python (3\.(11|12|13)(\.\d+)?)' can match incorrectly if the minor version starts with 11, 12, or 13 but has trailing digits (e.g., 3.110 or 3.120 in future releases). Adding a word boundary \b after the minor version ensures that only exact matches for 3.11, 3.12, or 3.13 are matched.

        if ($out -match 'Python (3\.(11|12|13)\b(\.\d+)?)') {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug?] Issue installing: No supported Python (3.11-3.13) found on this system

1 participant