Файли
openidea/src/features/settings/languages/PythonInterpreterPage.tsx
T
Muuтаarookieofc bff6a12e2f feat(python): manage every interpreter from a Python settings folder (#2987)
* feat(python): manage every interpreter from a Python settings folder

Settings › Languages & Frameworks now groups Python settings in one folder: All Interpreters (new), Dependencies (the project interpreter and its packages), Docstrings (split out of the interpreter page) and Ruff. All Interpreters lists interpreters detected on the machine, the ones other recently opened projects use and the ones added there, with a search field, PyCharm's "hide interpreters of other projects" and "show interpreter paths" toggles, and + / − to add an interpreter or remove it from the list (hidden, never deleted; a removed project interpreter falls back to the project virtualenv).

Each interpreter's source has its own icon (virtualenv, conda, uv, Poetry, pyenv/mise/asdf, system) in the list, the Add Interpreter dialog, the interpreter combo and the status bar menu. The IDE-wide list and its view options are global settings that a project cannot override; removed interpreters also disappear from the interpreter combo and the status bar popup.

* fix(python): compare interpreter paths by their own case rules and cap probes

A POSIX interpreter path kept only its own case (Linux allows `/opt/Python` and
`/opt/python` side by side); only Windows drive/UNC paths are folded now, and the
status bar uses the same key as the list. A configured `~/…` interpreter is matched
against the absolute path the backend resolves it to, so the "Project interpreter"
badge and removing it in use keep working without the frontend knowing the home
directory.

`sdk_python_project_interpreters` probes at most four recent projects at a time
instead of one thread per project, so opening All Interpreters does not start ~30
pythons at once. The hidden-but-in-use interpreter stays selectable in the combo and
status popup (custom entry / current fallback); that is now documented on `exclude`.

---------

Co-authored-by: arookieofc <[email protected]>
2026-11-13 08:56:17 +08:00

379 рядки
13 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/**
* Settings › Python › Dependencies (PyCharm's "Python Interpreter" page): the
* project interpreter and its packages (pip / uv pip). The interpreter feeds
* run configurations, basedpyright / pyright (`python.pythonPath`) and the
* terminal's virtualenv activation.
*/
import { isCancelled } from "@/platform/ipc";
import { useCallback, useEffect, useMemo, useState, type KeyboardEvent, type MouseEvent } from "react";
import clsx from "clsx";
import { Button, IconButton, Input, Spinner, showContextMenu, T } from "@/ui";
import { useProject } from "@/platform/project";
import { Section, Toggle } from "../components/Form";
import { effectivePython, listPackages, pip, type PythonPackage } from "./api";
import { AddInterpreterDialog } from "./AddInterpreterDialog";
import { useSetting } from "../components/context";
import { SdkField, tildify } from "./components";
import { interpreterIcon, sameInterpreterPath, usePythonInterpreters } from "./interpreters";
import styles from "./Sdk.module.css";
import { tx } from "@/platform/i18n";
type Op = "install" | "upgrade" | "uninstall";
interface Job {
op: Op;
target: string;
cancel: () => void;
}
export function filterPackages(packages: readonly PythonPackage[], query: string): PythonPackage[] {
const q = query.trim().toLowerCase();
return q ? packages.filter((p) => p.name.toLowerCase().includes(q)) : [...packages];
}
function usePackages(python: string | null) {
const [tick, setTick] = useState(0);
const [result, setResult] = useState<{
key: string;
python: string;
packages: PythonPackage[] | null;
error: string | null;
} | null>(null);
useEffect(() => {
if (!python) return;
let live = true;
const key = `${python}#${tick}`;
listPackages(python)
.then((packages) => live && setResult({ key, python, packages, error: null }))
.catch(
(e: unknown) => live && setResult({ key, python, packages: null, error: String((e as Error)?.message ?? e) }),
);
return () => {
live = false;
};
}, [python, tick]);
const reload = useCallback(() => setTick((t) => t + 1), []);
// While reloading the same interpreter, keep showing its previous list.
const same = !!python && result?.python === python;
return {
packages: same ? result.packages : null,
error: same ? result.error : null,
loading: !!python && result?.key !== `${python}#${tick}`,
reload,
};
}
export function PythonInterpreterPage() {
const [interpreter, setInterpreter] = useSetting<string>("languages.python.interpreter");
const [activate, setActivate] = useSetting<boolean>("languages.python.activateVirtualenv");
const root = useProject((s) => s.project?.root ?? null);
const [auto, setAuto] = useState<string | null>(null);
// Detected interpreters plus the ones added to (or hidden from) the IDE-wide list.
const { detected, rows, hidden } = usePythonInterpreters({ withProjects: false });
const extras = useMemo(
() =>
rows
.filter((row) => !row.invalid && !detected.some((sdk) => sameInterpreterPath(sdk.path, row.path, null)))
.map((row) => ({
value: row.path,
label: row.name,
detail: tildify(row.path),
icon: interpreterIcon(row.source),
})),
[rows, detected],
);
useEffect(() => {
let live = true;
effectivePython()
.then((p) => live && setAuto(p))
.catch(() => live && setAuto(null));
return () => {
live = false;
};
}, [root]);
// The interpreter runs resolve to: the configured one while it exists, else the fallback.
const [resolved, setResolved] = useState<{ interpreter: string; path: string | null } | null>(null);
useEffect(() => {
if (!interpreter) return;
let live = true;
effectivePython()
.then((path) => live && setResolved({ interpreter, path }))
.catch(() => live && setResolved(null));
return () => {
live = false;
};
}, [root, interpreter]);
// A configured interpreter whose executable is gone (PyCharm's "Invalid"): runs fall back.
const invalid =
!!interpreter && resolved?.interpreter === interpreter && !sameInterpreter(resolved.path, interpreter, root);
// Without a configured interpreter the project's virtualenv is used (like run configurations).
const python = interpreter || auto;
const { packages, error, loading, reload } = usePackages(python);
const [selected, setSelected] = useState<string | null>(null);
const [query, setQuery] = useState("");
const [installing, setInstalling] = useState(false);
const [requirement, setRequirement] = useState("");
const [job, setJob] = useState<Job | null>(null);
const [log, setLog] = useState<string[]>([]);
const [jobError, setJobError] = useState<string | null>(null);
const [adding, setAdding] = useState(false);
const shown = useMemo(() => filterPackages(packages ?? [], query), [packages, query]);
const run = (op: Op, target: string) => {
if (!python || job) return;
setLog([]);
setJobError(null);
const s = pip(python, target, op, (line) => setLog((l) => [...l.slice(-400), line]));
setJob({ op, target, cancel: s.cancel });
s.promise
.then(() => {
if (op === "install") {
setRequirement("");
setInstalling(false);
}
if (op === "uninstall") setSelected(null);
reload();
})
.catch((e: unknown) => setJobError(isCancelled(e) ? null : String((e as Error)?.message ?? e)))
.finally(() => setJob(null));
};
const menu = (e: MouseEvent, pkg: PythonPackage) => {
e.preventDefault();
setSelected(pkg.name);
showContextMenu(e, [
{ label: "Upgrade", icon: "arrow-up", disabled: !!job, run: () => run("upgrade", pkg.name) },
{ label: "Uninstall", icon: "minus", disabled: !!job, run: () => run("uninstall", pkg.name) },
]);
};
const onRowKey = (e: KeyboardEvent<HTMLTableRowElement>, i: number) => {
const next = e.key === "ArrowDown" ? i + 1 : e.key === "ArrowUp" ? i - 1 : -1;
if (next >= 0 && next < shown.length) {
e.preventDefault();
setSelected(shown[next]!.name);
(e.currentTarget.parentElement?.children[next] as HTMLElement | undefined)?.focus();
} else if ((e.key === "Delete" || e.key === "Backspace") && selected) {
e.preventDefault();
run("uninstall", selected);
}
};
const autoDetail = auto ? tildify(auto) : "No virtualenv in the project";
const current = detected?.find((s) => s.path === python);
return (
<div className={styles.page}>
<div className={styles.interpreterRow}>
<span className={styles.interpreterLabel}>
<T>Python Interpreter</T>
</span>
<SdkField
kind="python"
value={interpreter}
onChange={setInterpreter}
emptyLabel={auto ? "Project virtualenv (auto-detect)" : "<No interpreter>"}
emptyDetail={autoDetail}
browseTitle="Select Python Interpreter"
aria-label="Python interpreter"
invalid={invalid}
extra={extras}
exclude={hidden}
optionIcon={(sdk) => interpreterIcon(sdk.source)}
/>
<Button
size="small"
onClick={() => setAdding(true)}
disabled={!root}
title={root ? undefined : "Open a project first"}
>
<T>Add Interpreter…</T>
</Button>
</div>
{python && (
<div className={styles.path} title={python}>
{current && !invalid ? `${current.name} — ` : ""}
{tildify(python)}
</div>
)}
{invalid && (
<div className={clsx(styles.empty, styles.error)} role="alert">
<T>The configured Python interpreter no longer exists. Choose another interpreter or add a new one.</T>
</div>
)}
<div className={styles.tablePanel}>
<div className={styles.tableToolbar} role="toolbar" aria-label={tx("Packages")}>
<IconButton
icon="plus"
size="small"
title="Install"
disabled={!python || !!job}
pressed={installing}
onClick={() => setInstalling((v) => !v)}
/>
<IconButton
icon="minus"
size="small"
title="Uninstall"
disabled={!selected || !!job}
onClick={() => selected && run("uninstall", selected)}
/>
<IconButton
icon="arrow-up"
size="small"
title="Upgrade"
disabled={!selected || !!job}
onClick={() => selected && run("upgrade", selected)}
/>
<IconButton
icon="refresh"
size="small"
title="Reload List of Packages"
disabled={!python || loading}
onClick={reload}
/>
<span className={styles.spacer} />
{(loading || job) && <Spinner size={14} label={job ? `${job.op} ${job.target}` : "Loading packages"} />}
<Input
className={styles.search}
value={query}
placeholder="Search packages"
aria-label="Search packages"
onChange={(e) => setQuery(e.target.value)}
/>
</div>
{installing && (
<form
className={styles.installBar}
onSubmit={(e) => {
e.preventDefault();
if (requirement.trim()) run("install", requirement.trim());
}}
>
<Input
autoFocus
className={styles.installInput}
value={requirement}
placeholder="Package name or requirement, e.g. requests or django==5.1"
aria-label="Package to install"
onChange={(e) => setRequirement(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Escape") {
e.stopPropagation();
setInstalling(false);
}
}}
/>
<Button type="submit" size="small" variant="primary" disabled={!requirement.trim() || !!job}>
<T>Install Package</T>
</Button>
</form>
)}
<div className={styles.tableScroll}>
<table className={styles.table} aria-label={tx("Installed packages")}>
<thead>
<tr>
<th scope="col">
<T>Package</T>
</th>
<th scope="col">
<T>Version</T>
</th>
</tr>
</thead>
<tbody>
{shown.map((p, i) => (
<tr
key={p.name}
tabIndex={selected === p.name || (!selected && i === 0) ? 0 : -1}
aria-selected={selected === p.name}
className={clsx(selected === p.name && styles.selected)}
onMouseDown={() => setSelected(p.name)}
onContextMenu={(e) => menu(e, p)}
onKeyDown={(e) => onRowKey(e, i)}
>
<td>{p.name}</td>
<td className={styles.versionCell}>{p.version}</td>
</tr>
))}
</tbody>
</table>
{!python && (
<div className={styles.empty}>
<T>No interpreter selected. Choose one above or add a new one.</T>
</div>
)}
{python && error && !invalid && (
<div className={clsx(styles.empty, styles.error)} role="alert">
{error}
</div>
)}
{python && !error && packages && shown.length === 0 && (
<div className={styles.empty}>{query ? tx("No packages match") : tx("No packages installed")}</div>
)}
{python && !error && !packages && loading && (
<div className={styles.empty}>
<T>Loading packages…</T>
</div>
)}
</div>
</div>
{job && (
<div className={styles.status}>
<Spinner size={14} label="Working" />
<span>
{job.op === "install" ? tx("Installing") : job.op === "upgrade" ? tx("Upgrading") : tx("Uninstalling")}{" "}
{job.target}…
</span>
<Button size="small" variant="ghost" onClick={job.cancel}>
<T>Cancel</T>
</Button>
</div>
)}
{jobError && (
<div className={styles.error} role="alert">
{jobError}
</div>
)}
{log.length > 0 && (
<pre className={styles.log} aria-label={tx("pip output")}>
{log.join("\n")}
</pre>
)}
<Section title="Terminal">
<Toggle
label="Activate virtualenv"
checked={activate}
onChange={setActivate}
hint="New terminal sessions run the interpreter's virtualenv activate script."
/>
</Section>
{adding && root && (
<AddInterpreterDialog
root={root}
onClose={() => setAdding(false)}
onAdded={(path) => {
setInterpreter(path);
setAdding(false);
}}
/>
)}
</div>
);
}
/** Whether `resolved` (an absolute path from the backend) is the configured `interpreter` setting. */
function sameInterpreter(resolved: string | null, interpreter: string, root: string | null): boolean {
if (!resolved) return false;
if (tildify(resolved) === tildify(interpreter)) return true;
return !!root && resolved === `${root.replace(/[\\/]+$/, "")}/${interpreter}`;
}