mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
feat(run): Python run configuration type (#4126)
Adds PyCharm's Python configuration: a script or a -m module with arguments, command (interpreter or uv/poetry) and runner arguments; a plain python/python3 runs on the project interpreter as before. Existing python configurations (detected, imported, run from the context menu) open in this form when their command line maps to a script or module. projectPython prefers a detected python configuration over one made by hand. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
This commit is contained in:
co-authored by
wildcatDownstairs
parent
8959f7aa5d
commit
d7351dca1d
@@ -97,6 +97,7 @@ Open **Run › Edit Configurations…** (also in the run widget menu). The **Run
|
||||
| **Gradle** / **Maven** | Gradle tasks or Maven goals, optionally through the wrapper |
|
||||
| **Application** / **JAR Application** / **Spring Boot** | A Java main class, an executable JAR, a Spring Boot application |
|
||||
| **Tomcat Server** | A WAR application in a local Tomcat |
|
||||
| **Python** | A Python script or `-m` module |
|
||||
| **pytest** / **Django tests** | pytest tests, `manage.py test` |
|
||||
| **Ruby** / **Rake** / **RSpec** | A Ruby script, a Rake task, RSpec examples |
|
||||
| **PHPUnit** | PHPUnit tests |
|
||||
|
||||
@@ -105,7 +105,7 @@ In the editor, gutter icons run or debug:
|
||||
|
||||
A test file runs with pytest even when the project declares no test setup (`python -m pytest` with the project interpreter, so pytest must be installed there). In a Django project, `TestCase` tests can also run with `manage.py test`. Results of pytest and of Django's unittest runner appear in the test tree, where **Rerun Failed Tests** runs only the failures.
|
||||
|
||||
You can also create **pytest** and **Django tests** configurations in **Run › Edit Configurations…**. See [Run configurations](/en/guide/run) and [Testing](/en/guide/testing).
|
||||
You can also create **Python** (a script or module), **pytest** and **Django tests** configurations in **Run › Edit Configurations…**. See [Run configurations](/en/guide/run) and [Testing](/en/guide/testing).
|
||||
|
||||
| Action | macOS | Windows / Linux |
|
||||
| --------------------------- | ----- | ---------------- |
|
||||
|
||||
@@ -93,6 +93,7 @@ description: "创建、编辑和启动运行配置,从装订区运行代码,
|
||||
| **Gradle** / **Maven** | Gradle 任务或 Maven 目标,可选择通过 wrapper 运行 |
|
||||
| **应用程序** / **JAR 应用程序** / **Spring Boot** | Java 主类、可执行 JAR、Spring Boot 应用 |
|
||||
| **Tomcat 服务器** | 在本地 Tomcat 里运行 WAR 应用 |
|
||||
| **Python** | Python 脚本或 `-m` 模块 |
|
||||
| **pytest** / **Django 测试** | pytest 测试、`manage.py test` |
|
||||
| **Ruby 语言** / **Rake 任务** / **RSpec 测试** | Ruby 脚本、Rake 任务、RSpec 用例 |
|
||||
| **PHPUnit** | PHPUnit 测试 |
|
||||
|
||||
@@ -105,7 +105,7 @@ Ruff 只为位于 Ruff 配置之下的文件启动:`ruff.toml`、`.ruff.toml`
|
||||
|
||||
即使项目没有声明任何测试配置,测试文件也可以用 pytest 运行(用项目解释器执行 `python -m pytest`,因此该解释器里要装有 pytest)。在 Django 项目中,`TestCase` 测试也可以用 `manage.py test` 运行。pytest 和 Django unittest 运行器的结果显示在测试树中,**重新运行失败的测试** 只重跑失败的用例。
|
||||
|
||||
也可以在 **运行 › 编辑配置…** 中新建 **pytest** 和 **Django 测试** 配置。详见[运行配置](/zh/guide/run)和[测试](/zh/guide/testing)。
|
||||
也可以在 **运行 › 编辑配置…** 中新建 **Python**(脚本或模块)、**pytest** 和 **Django 测试** 配置。详见[运行配置](/zh/guide/run)和[测试](/zh/guide/testing)。
|
||||
|
||||
| 操作 | macOS | Windows / Linux |
|
||||
| -------------- | ----- | ---------------- |
|
||||
|
||||
@@ -397,6 +397,17 @@ describe("context configuration", () => {
|
||||
expect(contextConfig("tools/gen.py", uv)).toMatchObject({ command: "uv", args: ["run", "python", "tools/gen.py"] });
|
||||
});
|
||||
|
||||
it("prefers a detected Python config's interpreter over one made by hand", () => {
|
||||
const configs = [
|
||||
cfg({ id: "mine", kind: "python", command: "python3", args: ["tool.py"], detected: false }),
|
||||
cfg({ id: "found", kind: "python", command: "uv", args: ["run", "python", "-m", "app"], detected: true }),
|
||||
];
|
||||
expect(contextConfig("tools/gen.py", configs)).toMatchObject({
|
||||
command: "uv",
|
||||
args: ["run", "python", "tools/gen.py"],
|
||||
});
|
||||
});
|
||||
|
||||
it("takes the interpreter from a Django runserver config without its arguments", () => {
|
||||
const runserver = (command: string, args: string[]) =>
|
||||
cfg({ id: "django:runserver", kind: "python", command, args: [...args, "manage.py", "runserver"] });
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
import type { RunConfig } from "@/platform/ipc";
|
||||
import { djangoLabel, djangoTestsOf, hasDjangoTests } from "./djangoTypes";
|
||||
import { systemPython } from "./pythonTypes";
|
||||
import { phpunitArgs } from "./testing/phpunit";
|
||||
import { goTestParts } from "./goArguments";
|
||||
import { goBuildType } from "./goTypes";
|
||||
@@ -122,7 +123,8 @@ export function projectPython(configs: RunConfig[], relPath?: string): { command
|
||||
configs.filter((c) => ["python", "pytest", "django-test"].includes(c.kind)),
|
||||
relPath,
|
||||
);
|
||||
const py = configs.find((c) => c.kind === "python");
|
||||
// A detected configuration says what the project uses; one made by hand only what it runs itself.
|
||||
const py = configs.find((c) => c.kind === "python" && c.detected) ?? configs.find((c) => c.kind === "python");
|
||||
if (py) {
|
||||
// The interpreter part ends where the program starts: `-m`, or the script (`manage.py`). What follows
|
||||
// (`runserver`, `test`…) belongs to that program, never to the interpreter.
|
||||
@@ -144,11 +146,6 @@ export function projectPython(configs: RunConfig[], relPath?: string): { command
|
||||
return null;
|
||||
}
|
||||
|
||||
/** `python` on Windows (where `python3` is not a command), `python3` elsewhere. */
|
||||
function systemPython(): string {
|
||||
return isWindows() ? "python" : "python3";
|
||||
}
|
||||
|
||||
/**
|
||||
* PyCharm runs pytest for a `test_*.py` file even when the project declares no
|
||||
* test setup: `python -m pytest`, with the project's interpreter when detected.
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Python (PyCharm's "Python" configuration): a script or a `-m` module with its
|
||||
* arguments. The command is the interpreter (`python3`, `.venv/bin/python`, `uv`),
|
||||
* the runner args its leading arguments (`run python` for uv / poetry, `-u`). A plain
|
||||
* `python` / `python3` runs on the project interpreter: the backend swaps it at launch.
|
||||
*/
|
||||
import type { RunConfigTypeContribution } from "@/workbench/contributions";
|
||||
import { formatArgs, parseArgs } from "./configs";
|
||||
import { isWindows } from "./jvmTypes";
|
||||
|
||||
/** `python` on Windows (where `python3` is not a command), `python3` elsewhere. */
|
||||
export const systemPython = () => (isWindows() ? "python" : "python3");
|
||||
|
||||
export const pythonType: RunConfigTypeContribution = {
|
||||
id: "python",
|
||||
label: "Python",
|
||||
icon: "file-python",
|
||||
description: "Run a Python script or module",
|
||||
// A getter: the host's OS is known only once the project is open.
|
||||
get defaults() {
|
||||
return { mode: "script", command: systemPython() };
|
||||
},
|
||||
debugAdapter: "debugpy",
|
||||
fields: [
|
||||
{
|
||||
key: "mode",
|
||||
label: "Execute",
|
||||
control: "select",
|
||||
options: [
|
||||
{ value: "script", label: "Script file" },
|
||||
{ value: "module", label: "Module" },
|
||||
],
|
||||
},
|
||||
{
|
||||
key: "script",
|
||||
label: "Script path",
|
||||
mono: true,
|
||||
required: true,
|
||||
browse: "file",
|
||||
placeholder: "main.py",
|
||||
visible: (p) => p.mode !== "module",
|
||||
},
|
||||
{
|
||||
key: "module",
|
||||
label: "Module",
|
||||
mono: true,
|
||||
required: true,
|
||||
placeholder: "uvicorn",
|
||||
visible: (p) => p.mode === "module",
|
||||
},
|
||||
{ key: "args", label: "Arguments", mono: true, placeholder: "Space separated; quote with ' or \"" },
|
||||
{
|
||||
key: "command",
|
||||
label: "Command",
|
||||
mono: true,
|
||||
placeholder: "python3",
|
||||
required: true,
|
||||
hint: "e.g. python3, .venv/bin/python, uv, poetry",
|
||||
},
|
||||
{
|
||||
key: "runnerArgs",
|
||||
label: "Runner arguments",
|
||||
mono: true,
|
||||
placeholder: "run python",
|
||||
hint: "e.g. `run python` for uv/poetry; empty calls the interpreter itself",
|
||||
},
|
||||
],
|
||||
commandLine(p) {
|
||||
const module = p.mode === "module";
|
||||
const target = (module ? p.module : p.script)?.trim();
|
||||
const program = !target ? [] : module ? ["-m", target] : [target];
|
||||
return {
|
||||
command: p.command?.trim() || systemPython(),
|
||||
args: [...parseArgs(p.runnerArgs ?? ""), ...program, ...parseArgs(p.args ?? "")],
|
||||
};
|
||||
},
|
||||
paramsFrom(c) {
|
||||
if (c.kind !== "python") return null;
|
||||
// The program starts at `-m` or the script; what precedes it belongs to the interpreter.
|
||||
const at = c.args.findIndex((a) => a === "-m" || /\.pyw?$/.test(a));
|
||||
if (at < 0) return null;
|
||||
const module = c.args[at] === "-m";
|
||||
const target = c.args[module ? at + 1 : at];
|
||||
if (!target) return null;
|
||||
return {
|
||||
mode: module ? "module" : "script",
|
||||
[module ? "module" : "script"]: target,
|
||||
args: formatArgs(c.args.slice(at + (module ? 2 : 1))),
|
||||
command: c.command,
|
||||
runnerArgs: formatArgs(c.args.slice(0, at)),
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -355,6 +355,47 @@ describe("configuration types", () => {
|
||||
expect(rake(["-T"])).toMatchObject({ options: "-T", task: "" });
|
||||
});
|
||||
|
||||
it("Python runs a script or module and round-trips detected command lines", () => {
|
||||
const python = typeFor("python")!;
|
||||
expect(newConfigOfType(python, [])).toMatchObject({
|
||||
kind: "python",
|
||||
command: "python3",
|
||||
args: [],
|
||||
isTest: false,
|
||||
debugAdapter: "debugpy",
|
||||
});
|
||||
expect(missingParams(newConfigOfType(python, []), python)).toHaveProperty("script");
|
||||
expect(
|
||||
python.commandLine({ mode: "module", module: " uvicorn ", script: "ignored.py", args: "app:app --reload" }),
|
||||
).toEqual({ command: "python3", args: ["-m", "uvicorn", "app:app", "--reload"] });
|
||||
|
||||
const lines: Array<{ command: string; args: string[]; params: Record<string, string> }> = [
|
||||
{
|
||||
command: ".venv/bin/python",
|
||||
args: ["-u", "src/my app.py", "--port", "8000"],
|
||||
params: { mode: "script", script: "src/my app.py", runnerArgs: "-u", args: "--port 8000" },
|
||||
},
|
||||
{
|
||||
command: "uv",
|
||||
args: ["run", "python", "manage.py", "runserver"],
|
||||
params: { mode: "script", script: "manage.py", runnerArgs: "run python", args: "runserver" },
|
||||
},
|
||||
{
|
||||
command: "python3",
|
||||
args: ["-X", "utf8", "-m", "pkg.cli", "serve"],
|
||||
params: { mode: "module", module: "pkg.cli", runnerArgs: "-X utf8", args: "serve" },
|
||||
},
|
||||
];
|
||||
for (const { command, args, params } of lines) {
|
||||
const config = { ...newConfigOfType(python, []), command, args, params: undefined };
|
||||
expect(formParams(config, python)).toMatchObject({ command, ...params });
|
||||
}
|
||||
// No script or module to show in the form: it stays a plain command.
|
||||
expect(
|
||||
formParams({ ...newConfigOfType(python, []), args: ["-c", "print(1)"], params: undefined }, python),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("Django tests round-trip commandLine / paramsFrom and always report to the test tree", () => {
|
||||
const django = typeFor("django-test")!;
|
||||
expect(django.debugAdapter).toBe("debugpy");
|
||||
|
||||
@@ -27,6 +27,7 @@ import { goBuildType, goTestType } from "./goTypes";
|
||||
import { dotnetProjectType, dotnetTestType } from "./dotnetTypes";
|
||||
import { rakeType, rspecType, rubyScriptType } from "./rubyTypes";
|
||||
import { djangoTestType } from "./djangoTypes";
|
||||
import { pythonType } from "./pythonTypes";
|
||||
import { phpunitType } from "./phpunitTypes";
|
||||
import { UNITY_CONFIG_KIND } from "./unity";
|
||||
|
||||
@@ -491,6 +492,7 @@ export const BUILTIN_TYPES: readonly RunConfigTypeContribution[] = [
|
||||
gradle,
|
||||
maven,
|
||||
godot,
|
||||
pythonType,
|
||||
pytest,
|
||||
rubyScriptType,
|
||||
rakeType,
|
||||
|
||||
@@ -2456,5 +2456,6 @@
|
||||
"Load Maven Project": "Load Maven Project",
|
||||
"Skip": "Skip",
|
||||
"Resize Markdown preview": "Resize Markdown preview",
|
||||
"Run a Python script or module": "Run a Python script or module",
|
||||
"Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed.": "Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed."
|
||||
}
|
||||
|
||||
@@ -5749,6 +5749,7 @@
|
||||
"Archive ({count})": "归档({count})",
|
||||
"Delete ({count})…": "删除({count})…",
|
||||
"Run pytest tests": "运行 pytest 测试",
|
||||
"Run a Python script or module": "运行 Python 脚本或模块",
|
||||
"e.g. pytest, uv, poetry, .venv/bin/pytest, python": "例如 pytest、uv、poetry、.venv/bin/pytest、python",
|
||||
"Runner arguments": "运行器实参",
|
||||
"e.g. `run pytest` for uv/poetry, `-m pytest` for python; empty calls the command itself": "例如 uv/poetry 使用 `run pytest`,python 使用 `-m pytest`;留空则直接调用命令本身",
|
||||
|
||||
@@ -258,6 +258,7 @@ const UNTRANSLATED = new Set<string>([
|
||||
"go env GOPATH",
|
||||
"kebab-case",
|
||||
"KEY=value; OTHER=value",
|
||||
"main.py",
|
||||
"my-mcp-server",
|
||||
"mysite.settings_test",
|
||||
"NAME",
|
||||
@@ -280,6 +281,7 @@ const UNTRANSLATED = new Set<string>([
|
||||
"run python",
|
||||
"snake_case",
|
||||
"tests",
|
||||
"uvicorn",
|
||||
'{ "section": { "key": "value" } }',
|
||||
// Identifiers that only look like text: icon names, error codes, categories, console output
|
||||
"app",
|
||||
|
||||
Reference in New Issue
Block a user