现象:编辑器 / pyright 满屏 reportMissingImports,但 .venv 与 conda 环境里 import numpy, pandas 都正常,pytest 也全过。 根因两层: 1. pyright 没配 venvPath / pythonPath 时会去 PATH 上找 python。本机 PATH 上 唯一的 python 是 agent-tools 的 shim(Codex 运行时,Python 3.14),没有科学栈。 pyright --verbose 显示它选了 3.14.6,Search paths 里一个 Anaconda 路径都没有。 注意持久化的 User/Machine PATH 并没有这一项,是 Codex 进程临时注入的——所以 "我的终端没问题"和"工具有问题"可以同时成立。 2. .venv 是 --system-site-packages 建的,三方包在 D:\anaconda\Lib\site-packages, 而 pyright / Pylance 不认 pyvenv.cfg 的 include-system-site-packages, 只去 .venv\Lib\site-packages 找(那里只有 pip)。 pyright 不读 pyvenv.cfg,但**认 .pth**。 改动: - pyproject.toml [tool.pyright]:venvPath = "." / venv = ".venv" / pythonVersion = "3.13",并注释了为什么必须写 - scripts/setup-venv.ps1:新增第 [2/4] 步,自动往 .venv 写 _mcm_base_site_packages.pth 指向该机 base 的 site-packages,换机重建即可 - tests/test_data.py:3 处 isna().all() 断言外面套 bool()(它的类型是 Series | bool,直接当条件 pyright 报"条件操作数无效"); 2 处故意传错类型的用例加 type: ignore[arg-type] 结果:pyright 17 errors -> 0 errors / 0 warnings;pytest 两套环境均 40 passed, 44 skipped;ruff All checks passed;setup-venv.ps1 通过 AST 解析 (纯 ASCII + LF)。 文档:docs/04-environment.md 新增坑 7(含 pyright --verbose 诊断口诀与 VS Code / PyCharm 的操作步骤)、STATUS 决策 29、CHANGELOG。
83 lines
3.4 KiB
PowerShell
83 lines
3.4 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
Create .venv inside the repo and install mcm-toolkit in editable mode.
|
|
|
|
.DESCRIPTION
|
|
Uses --system-site-packages so the venv reuses the scientific stack already
|
|
installed in Anaconda base. That makes the whole step OFFLINE: no package is
|
|
downloaded, yet numpy / scipy / pandas / matplotlib / sklearn / statsmodels /
|
|
networkx / sympy are all importable.
|
|
|
|
The editable install uses --no-deps --no-build-isolation: dependencies are
|
|
already provided by base, and pip must not go online for a build backend.
|
|
|
|
NOTE: this file is intentionally pure ASCII and UTF-8 without BOM.
|
|
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 Python
|
|
Interpreter used to create the venv. Default: D:\anaconda\python.exe
|
|
|
|
.PARAMETER Recreate
|
|
Delete and recreate .venv if it already exists.
|
|
|
|
.EXAMPLE
|
|
.\scripts\setup-venv.ps1
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[string]$Python = 'D:\anaconda\python.exe',
|
|
[switch]$Recreate
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$repoRoot = Split-Path -Parent $PSScriptRoot
|
|
$venvDir = Join-Path $repoRoot '.venv'
|
|
|
|
if (-not (Test-Path -LiteralPath $Python)) {
|
|
$cmd = Get-Command python -ErrorAction SilentlyContinue
|
|
if (-not $cmd) { throw "Python interpreter not found: $Python" }
|
|
$Python = $cmd.Source
|
|
Write-Host "Using python from PATH: $Python" -ForegroundColor Yellow
|
|
}
|
|
|
|
if (Test-Path -LiteralPath $venvDir) {
|
|
if (-not $Recreate) {
|
|
Write-Host '.venv already exists. Use -Recreate to rebuild.' -ForegroundColor Yellow
|
|
} else {
|
|
Write-Host 'Removing existing .venv ...'
|
|
Remove-Item -LiteralPath $venvDir -Recurse -Force
|
|
}
|
|
}
|
|
|
|
if (-not (Test-Path -LiteralPath $venvDir)) {
|
|
Write-Host '[1/4] creating .venv (--system-site-packages, reuses Anaconda base) ...'
|
|
& $Python -m venv --system-site-packages --prompt 'mcm-toolkit' $venvDir
|
|
if ($LASTEXITCODE -ne 0) { throw 'venv creation failed' }
|
|
}
|
|
|
|
$venvPy = Join-Path $venvDir 'Scripts\python.exe'
|
|
$venvSitePackages = Join-Path $venvDir 'Lib\site-packages'
|
|
|
|
Write-Host '[2/4] linking base site-packages for static analyzers (.pth) ...'
|
|
# pyright / Pylance do NOT honour "include-system-site-packages" in pyvenv.cfg,
|
|
# but they DO read *.pth files found in site-packages. Without this file they see
|
|
# only .venv\Lib\site-packages (which holds just pip), so every "import numpy"
|
|
# in the editor is reported as unresolved even though the interpreter is right.
|
|
$baseSitePackages = (& $Python -c "import sysconfig; print(sysconfig.get_paths()['purelib'])").Trim()
|
|
if (-not (Test-Path -LiteralPath $baseSitePackages)) { throw "base site-packages not found: $baseSitePackages" }
|
|
Set-Content -LiteralPath (Join-Path $venvSitePackages '_mcm_base_site_packages.pth') -Value $baseSitePackages -Encoding ASCII
|
|
Write-Host " -> $baseSitePackages"
|
|
|
|
Write-Host '[3/4] editable install of mcm-toolkit (--no-deps --no-build-isolation) ...'
|
|
& $venvPy -m pip install -e $repoRoot --no-deps --no-build-isolation --disable-pip-version-check
|
|
if ($LASTEXITCODE -ne 0) { throw 'pip install -e failed' }
|
|
|
|
Write-Host '[4/4] verifying ...'
|
|
& $venvPy -c "import mcm_toolkit, numpy, scipy, pandas, matplotlib; print('mcm_toolkit', mcm_toolkit.__version__); print('numpy', numpy.__version__)"
|
|
|
|
Write-Host ''
|
|
Write-Host 'Done. Activate with:' -ForegroundColor Green
|
|
Write-Host ' .\.venv\Scripts\Activate.ps1'
|