Description
https://peps.python.org/pep-0723/
Python has adopted a format for putting dependencies into a script header and python tooling has implemented ways to run it!
here is an example of uv being used to run a script. It will parse the /// script
header, use the specified version of python, create a virtual environment, install dependencies, and then execute the script.
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "requests", "pydantic",
# ]
# ///
# (note, this is tested on macOS, I don't know if env -S works on gnu systems)
import requests
print(requests.get("example.org"))
right now vscode-python doesn't really know how to handle several aspects of this.
first is language detection. scripts often don't have a file extension so it would be nice to detect uv
or pipx
in the shabang line to mean "probably python". you could also do more advanced parsing of the entire /// script
block.
next is code intelligence, without pythonInterpreter
detection there are red squiggles everywhere. when run scripts get their own venv created in a temp location, unfortunately this behavior is tool specific. maybe vscode could create/manage its own!
thanks!