Exploring UV: self-contained Python scripts made easy

Mar 28, 2025

Last night, I experimented with UV and discovered a great feature — it allows you to create self-contained Python scripts!

These scripts can include information about the required Python version and dependencies, making them highly portable. And with the right shebang, you don’t even need to worry about managing virtual environments or manually installing packages. As long as UV is installed, it takes care of everything for you.

For example, you can create a script like this:

#!/usr/bin/env -S uv run --script

# /// script
# requires-python = ">=3.13"
# dependencies = [
#   "boto3==1.37.22"
# ]
# ///

import sys
import boto3

print(f"Pyhton version: {sys.version}")
print(f"Boto version: {boto3.__version__}")

Make it executable:

chmod +x test_uv.py

And just run it:

./test_uv.py 
Installed 7 packages in 23ms
Pyhton version: 3.13.2 (main, Feb  5 2025, 19:11:32) [Clang 19.1.6 ]
Boto version: 1.37.22

UV will automatically handle dependencies and execute the script as expected, making it a convenient tool for simplifying small Python script setups.