#Requires -Version 5.1 <# .SYNOPSIS Environment self-check: interpreters, conda envs, dependencies, package import, tests. .DESCRIPTION Run this on day one of a contest so environment problems surface before you start working, instead of three hours before the deadline. Checks both environments: * .venv lightweight, offline-safe (10 MB) * conda env "mcm-toolkit" the main working environment NOTE: this file is intentionally pure ASCII, UTF-8 without BOM, LF endings. Windows PowerShell 5.1 parses .ps1 as ANSI when there is no BOM, so non-ASCII characters (e.g. Chinese) turn into illegal bytes and cause syntax errors. .PARAMETER SkipTests Do not run pytest. .EXAMPLE .\scripts\doctor.ps1 #> [CmdletBinding()] param( [switch]$SkipTests ) $ErrorActionPreference = 'Continue' $repoRoot = Split-Path -Parent $PSScriptRoot # Python snippet kept as a single-quoted here-string: it must not contain double # quotes, because PowerShell 5.1 strips them when passing arguments to native programs. $probe = @' import importlib mods = ['numpy','scipy','pandas','matplotlib','sklearn','statsmodels', 'networkx','sympy','pytest','ruff','SALib','pymoo','pyright','openpyxl'] for name in mods: try: mod = importlib.import_module(name) ver = getattr(mod, '__version__', '') print(' OK {:12s} {}'.format(name, ver)) except Exception as exc: print(' MISS {:12s} ({})'.format(name, type(exc).__name__)) '@ function Report-Interpreter { param([string]$Label, [string]$Py) Write-Host '' Write-Host "--- $Label ---" -ForegroundColor Cyan if (-not (Test-Path -LiteralPath $Py)) { Write-Host " not found: $Py" -ForegroundColor Yellow return } & $Py -c "import sys; print(' exe :', sys.executable); print(' python:', sys.version.split()[0])" & $Py -c $probe & $Py -c "import mcm_toolkit as m; print(' mcm_toolkit', m.__version__, '/', len(m.__all__), 'public names')" } Write-Host '=== MCM/ICM Toolkit environment check ===' -ForegroundColor Cyan Write-Host "repo: $repoRoot" Report-Interpreter -Label 'venv (.venv) - lightweight / offline-safe' -Py (Join-Path $repoRoot '.venv\Scripts\python.exe') $conda = Get-Command conda -ErrorAction SilentlyContinue $condaExe = if ($conda) { $conda.Source } else { $null } if (-not $condaExe) { foreach ($c in @('D:\anaconda\Scripts\conda.exe', "$env:USERPROFILE\anaconda3\Scripts\conda.exe")) { if (Test-Path -LiteralPath $c) { $condaExe = $c; break } } } if ($condaExe) { Write-Host '' Write-Host "--- conda ($condaExe) ---" -ForegroundColor Cyan & $condaExe --version | ForEach-Object { Write-Host " $_" } Write-Host ' environments:' & $condaExe env list | ForEach-Object { Write-Host " $_" } $condaPy = Join-Path (Split-Path -Parent (Split-Path -Parent $condaExe)) 'envs\mcm-toolkit\python.exe' Report-Interpreter -Label 'conda env "mcm-toolkit" - main working env' -Py $condaPy } else { Write-Host '' Write-Host '--- conda ---' -ForegroundColor Cyan Write-Host ' conda not found on PATH or in the usual locations' -ForegroundColor Yellow } if (-not $SkipTests) { Write-Host '' Write-Host '--- tests ---' -ForegroundColor Cyan Push-Location $repoRoot foreach ($py in @((Join-Path $repoRoot '.venv\Scripts\python.exe'), $condaPy)) { if ($py -and (Test-Path -LiteralPath $py)) { Write-Host " $py" & $py -m pytest -p no:cacheprovider -q 2>&1 | Select-Object -Last 1 | ForEach-Object { Write-Host " $_" } } } Pop-Location } Write-Host '' Write-Host 'Check finished.' -ForegroundColor Cyan