Run Python files with 'uv run' in VS Code and Cursor
If you use uv
to manage Python dependencies, the builtβin "Run Python File" action in VS Code/Cursor will still execute with your selected interpreter, not with uv run
. Even if you point the Python interpreter at a uv venv
, uv
scripts still need to be run via uv run foo.py
so that dependencies and environment are resolved correctly.
Here is how I setup a quick way to run the active file with uv run
without opening the terminal every time. Create .vscode/tasks.json
with this content:
{
"version": "2.0.0",
"tasks": [
{
"label": "uv: run active file",
"type": "shell",
"command": "uv",
"args": ["run", "${fileBasename}"],
"options": {
// Run in the file's directory so imports and relative paths work
"cwd": "${fileDirname}"
},
"problemMatcher": [],
// Make this the default task so you can press Ctrl+Shift+B
"group": { "kind": "build", "isDefault": true }
}
]
}
This adds a task called uv: run active file
. Because itβs the default build task, you can run it with the standard shortcut: Ctrl+Shift+B
. You can also run it from the menu: Terminal β Run Build Task β uv: run active file
, but that's not so convenient.
I also wanted a oneβclick button, so I installed the Tasks
extension by actboy168. Then added a statusbar
section to the same task:
{
"version": "2.0.0",
"tasks": [
{
"label": "uv: run active file",
// ...
// Requires the "Tasks" extension by actboy168
"statusbar": { "label": "$(play) uv run" }
}
]
}
This adds a playβicon button to the status bar that runs the active file with uv run
.
I also didn't love the Ctrl+Shift+B shortcut, so I added a keybinding to Ctrl+B
to run the task.
// In command palette: "Preferences: Open Keyboard Shortcuts (JSON)"
{ "key": "ctrl+b", "command": "workbench.action.tasks.runTask", "args": "uv: run active file", "when": "editorTextFocus" }
What didn't work
I couldn't get the Run Python File
button to run with the uv
interpreter. I also couldn't add a button to the Tab menu next to the Run Python File
button.
There's an option to use Python Debugger: Debug using launch.json
, but I don't always want to debug.