Self-Contained Python Scripts with uv
Self-Contained Python Scripts with uv
April 4, 2025
I saw this from Tucker’s
and Dave’s
articles.
Basically you can create a Python script, include what are the required dependencies and using uv, be able
to run the script and get the dependencies all in one go.
To add a dependency to a script you can do the following:
> uv add --script your_script.py httpxThis will include httpx as a dependency:
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# ]
# ///
import argparse
import asyncioTo run this script:
> uv run your_script.pyAlso if you put the following on the first line in the script:
#!/usr/bin/env python
...or
#!/usr/bin/env -S uv run --script
...Now the script can be executable (chmod +x your_script.py).
Note won’t work in PowerShell but should work in Unix-like systems.
However this could work with the py launcher if you do the following:
#!/usr/bin/env -S uv run --script
...