+
+ {row.name}
+ {installed ?? Not installed}
+
+ {info.found?.summary &&
{info.found.summary}
}
+ {!readOnly && !asked && (
+
+
+
+ )}
+ {asked && info.loading && (
+
+ Loading package information from PyPI…
+
+ )}
+ {asked && info.error && (
+
+
+ Could not load package information from PyPI.
+ {" "}
+
+
+ )}
+ {!readOnly && asked && !info.loading && !info.error && !info.found && (
+
+ This package is not on PyPI.
+
+ )}
+ {!readOnly && (
+
+ {options.length > 0 && version && (
+
+ )}
+ {installed === null ? (
+
+ ) : (
+ <>
+
+ {row.latest && (
+
+ )}
+
+ >
+ )}
+
+ )}
+
+ );
+}
diff --git a/src/features/python-packages/PythonPackages.module.css b/src/features/python-packages/PythonPackages.module.css
new file mode 100644
index 0000000000..2792e72172
--- /dev/null
+++ b/src/features/python-packages/PythonPackages.module.css
@@ -0,0 +1,133 @@
+.listPane,
+.detailsPane {
+ display: flex;
+ flex-direction: column;
+ min-height: 0;
+ background: var(--bg-editor);
+}
+.detailsPane {
+ gap: var(--space-2);
+ padding: var(--space-2) var(--space-3);
+ overflow: auto;
+}
+
+.toolbar {
+ flex: none;
+ display: flex;
+ align-items: center;
+ gap: var(--space-1-5);
+ padding: var(--space-1) var(--space-2);
+ border-bottom: 1px solid var(--border);
+ background: var(--bg-elev);
+}
+.search {
+ flex: 1;
+ min-width: 0;
+}
+.interpreter {
+ flex: 0 1 auto;
+ min-width: 0;
+ max-width: 40%;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ color: var(--fg-muted);
+ font-size: var(--fs-small);
+}
+.list {
+ flex: 1;
+ min-height: 0;
+}
+
+.row {
+ display: flex;
+ align-items: baseline;
+ gap: var(--space-2);
+ min-width: 0;
+ width: 100%;
+}
+.name {
+ flex: 1;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+.version {
+ color: var(--fg-secondary);
+ font-variant-numeric: tabular-nums;
+}
+.upgradable {
+ display: inline-flex;
+ align-items: center;
+ gap: var(--space-1);
+ color: var(--link);
+ font-variant-numeric: tabular-nums;
+}
+
+.details {
+ display: flex;
+ flex-direction: column;
+ gap: var(--space-2);
+}
+.title {
+ display: flex;
+ align-items: baseline;
+ gap: var(--space-2);
+}
+.packageName {
+ font-size: var(--fs-input-large);
+ font-weight: 600;
+ user-select: text;
+ -webkit-user-select: text;
+}
+.summary {
+ color: var(--fg-secondary);
+ user-select: text;
+ -webkit-user-select: text;
+}
+.actions {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: var(--space-1-5);
+}
+.versions {
+ min-width: 140px;
+}
+
+.note {
+ color: var(--fg-subtle);
+ font-size: var(--fs-small);
+ line-height: 1.4;
+}
+.listPane > .note {
+ padding: var(--space-1) var(--space-2);
+ border-top: 1px solid var(--border);
+}
+.status {
+ display: flex;
+ align-items: center;
+ gap: var(--space-1-5);
+ color: var(--fg-secondary);
+}
+.error {
+ padding: var(--space-1) var(--space-2);
+ color: var(--danger);
+ white-space: pre-wrap;
+}
+.log {
+ flex: 1;
+ min-height: 60px;
+ margin: 0;
+ padding: var(--space-1) var(--space-2);
+ overflow: auto;
+ border: 1px solid var(--border);
+ border-radius: var(--radius-small);
+ font-family: var(--font-mono);
+ font-size: var(--fs-small);
+ white-space: pre-wrap;
+ overflow-wrap: anywhere;
+ color: var(--fg-secondary);
+ user-select: text;
+ -webkit-user-select: text;
+}
diff --git a/src/features/python-packages/index.ts b/src/features/python-packages/index.ts
new file mode 100644
index 0000000000..5fdc18719c
--- /dev/null
+++ b/src/features/python-packages/index.ts
@@ -0,0 +1,31 @@
+/** Python Packages: the project interpreter's packages, with PyPI lookup, in a tool window. */
+import { registerActions } from "@/platform/actions";
+import { lazyComponent } from "@/ui";
+import { registerToolWindow, toolWindowService } from "@/workbench/contributions";
+
+const ID = "pythonPackages";
+
+export function activate(): () => void {
+ const disposers = [
+ registerToolWindow({
+ id: ID,
+ title: "Python Packages",
+ icon: "package",
+ anchor: "bottom",
+ // Listed under the stripe's "…" until first opened: most projects are not Python ones.
+ stripe: "onUse",
+ order: 28,
+ component: lazyComponent(() => import("./PackagesView").then((m) => m.PackagesView)),
+ }),
+ registerActions([
+ {
+ id: "python.packages.show",
+ title: "Python Packages",
+ category: "Tools",
+ when: "projectOpen",
+ run: () => toolWindowService().show(ID, true),
+ },
+ ]),
+ ];
+ return () => disposers.reverse().forEach((d) => d());
+}
diff --git a/src/features/python-packages/model.test.ts b/src/features/python-packages/model.test.ts
new file mode 100644
index 0000000000..61adf45f76
--- /dev/null
+++ b/src/features/python-packages/model.test.ts
@@ -0,0 +1,56 @@
+import { describe, expect, it } from "vitest";
+import { isPackageName, listRows, normalize, requirementOf, rowKey } from "./model";
+
+const INSTALLED = [
+ { name: "requests", version: "2.31.0" },
+ { name: "typing_extensions", version: "4.12.0" },
+];
+const found = (name: string) => ({ name, summary: "", latest: "1.0", versions: ["1.0"] });
+
+describe("package rows", () => {
+ it("lists the installed packages with their newer release", () => {
+ const rows = listRows(INSTALLED, new Map([["requests", "2.34.2"]]), null, "");
+ expect(rows.map(rowKey)).toEqual(["section:installed", "installed:requests", "installed:typing_extensions"]);
+ expect(rows[1]).toMatchObject({ version: "2.31.0", latest: "2.34.2" });
+ expect(rows[2]).toMatchObject({ latest: null });
+ });
+
+ it("filters the installed packages and adds the PyPI project the query names", () => {
+ const rows = listRows(INSTALLED, null, found("six"), " SIX ");
+ expect(rows.map(rowKey)).toEqual(["section:pypi", "pypi:six"]);
+ expect(listRows(INSTALLED, null, null, "quest").map(rowKey)).toEqual(["section:installed", "installed:requests"]);
+ });
+
+ it("filters the installed packages by their normalised names", () => {
+ for (const query of ["typing-extensions", "Typing.Extensions", "typing_ext"]) {
+ expect(listRows(INSTALLED, null, found("typing-extensions"), query).map(rowKey)).toEqual([
+ "section:installed",
+ "installed:typing_extensions",
+ ]);
+ }
+ });
+
+ it("does not list an installed package again under PyPI, however its name is spelled", () => {
+ const rows = listRows(INSTALLED, null, found("typing-extensions"), "typing");
+ expect(rows.map(rowKey)).toEqual(["section:installed", "installed:typing_extensions"]);
+ });
+});
+
+describe("names and requirements", () => {
+ it("compares names as PEP 503 does", () => {
+ expect(normalize("Typing_Extensions")).toBe("typing-extensions");
+ expect(normalize("zope.interface")).toBe("zope-interface");
+ });
+
+ it("asks PyPI only for text that can be a name", () => {
+ for (const ok of ["six", "zope.interface", "a", "Django"]) expect(isPackageName(ok)).toBe(true);
+ for (const bad of ["", "-e", "pip-", "requests==2.0", "a b", "git+https://x", "a".repeat(129)]) {
+ expect(isPackageName(bad)).toBe(false);
+ }
+ });
+
+ it("pins a chosen version", () => {
+ expect(requirementOf("six", "1.16.0")).toBe("six==1.16.0");
+ expect(requirementOf("six", null)).toBe("six");
+ });
+});
diff --git a/src/features/python-packages/model.ts b/src/features/python-packages/model.ts
new file mode 100644
index 0000000000..6980c96b8f
--- /dev/null
+++ b/src/features/python-packages/model.ts
@@ -0,0 +1,49 @@
+/** What the Python Packages tool window lists, and the requirement an operation installs. */
+import type { PypiPackage, PythonPackage } from "@/platform/ipc";
+
+/** How pip and PyPI compare names (PEP 503). */
+export const normalize = (name: string) => name.toLowerCase().replace(/[-_.]+/g, "-");
+
+/** Whether `text` can be a distribution name, so it is worth asking PyPI for. */
+export const isPackageName = (text: string) =>
+ text.length <= 128 && /^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?$/.test(text);
+
+export type Row =
+ | { kind: "section"; id: "installed" | "pypi" }
+ | { kind: "installed"; name: string; version: string; latest: string | null }
+ | { kind: "pypi"; name: string; latest: string };
+
+export const rowKey = (row: Row) => (row.kind === "section" ? `section:${row.id}` : `${row.kind}:${row.name}`);
+
+/**
+ * The installed packages matching `query`, then the PyPI project the query names when it
+ * is not installed. `newer` maps a package to its newer release.
+ */
+export function listRows(
+ installed: readonly PythonPackage[],
+ newer: ReadonlyMap