Streamlining Python Script Execution with uv and PEP 723

2025-06-24
ℹ️Note on the source

This blog post was automatically generated (and translated). It is based on the following original, which I selected for publication on this blog:
Fun with uv and PEP 723.

Simplifying Python Script Execution

Historically, executing Python scripts required careful environment setup, including Python version management and dependency installation. However, recent advancements have streamlined this process, making Python more accessible for one-off scripting tasks.

Introducing uv

uv emerges as a fast Python package and project manager, written in Rust. Its key feature is the uv run command, which functions similarly to npx in the Node.js ecosystem. It enables the invocation of Python tools within packages, handling the creation of disposable virtual environments, Python version selection, and dependency installation.

uv run pip install pandas
Installed 1 package in 5ms

PEP 723: Inline Script Metadata

PEP 723 introduces a standardized metadata format embedded within single-file Python scripts. This metadata assists launchers, IDEs, and other external tools in interacting with these scripts.

Here's an example:

#!/usr/bin/env python3

  PEP723   = {
 "build-system": {
 "requires": ["requests >= 2.28.1"],
 "build-backend": "setuptools.build_meta",
 },
}

import requests

print(requests.get("https://example.com").status_code)

Combining uv and PEP 723

By integrating uv with PEP 723 metadata, Python scripts can be executed directly without manual environment configuration. Consider the following command:

uv run ./script.py

This command automatically creates a virtual environment, installs necessary dependencies specified in the script's metadata, and executes the script.

Installed 9 packages in 24ms

│ ('1', 'PEP Purpose and Guidelines'),

│ ('2', 'Procedure for Adding New Modules'),

│ ('3', 'Guidelines for Handling Bug Reports'),

│ ('4', 'Deprecation of Standard Modules'),

│ ('5', 'Guidelines for Language Evolution'),

│ ('6', 'Bug Fix Releases'),

│ ('7', 'Style Guide for C Code'),

│ ('8', 'Style Guide for Python Code'),

│ ('9', 'Sample Plaintext PEP Template'),

│ ('10', 'Voting Guidelines')

Practical Application: YouTube Transcript Extraction

These technologies can be combined to create executable scripts for various tasks. For example, consider a script to extract YouTube transcripts:

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

  PEP723   = {
 "dependencies": [
 "youtube-transcript-api"
 ]
}

import sys
from youtube_transcript_api import YouTubeTranscriptApi

def get_video_id(url):
 from urllib.parse import urlparse, parse_qs
 parsed_url = urlparse(url)
 if parsed_url.netloc == 'www.youtube.com':
 query = parse_qs(parsed_url.query)
 if 'v' in query:
 return query['v'][0]
 return url

video_url = sys.argv[1]
video_id = get_video_id(video_url)

try:
 transcript = YouTubeTranscriptApi.get_transcript(video_id)
 for entry in transcript:
 print(entry['text'])
except Exception as e:
 print(f"Error: {e}")

This script, when saved as youtube_transcript.py and made executable, can be run as follows:

./youtube_transcript.py <YouTube_URL_or_video_ID>

The shebang line #!/usr/bin/env -S uv run --script is crucial, especially with the --script flag.

Installed 7 packages in 10ms

hey it's Matt here and today I'm going

to show you how to use UV not only to

install packages in your python projects

but to manage entire projects to create

virtual environments and even how to

manage entire versions of python with UV

uh and hopefully you'll understand by

any of this video UV is a dropin

Implications and Future Possibilities

This integration of uv and PEP 723 simplifies Python script execution, opening up new possibilities for seamless automation and task management. The ability to create self-contained, executable Python scripts streamlines workflows and reduces the overhead associated with environment management.

Could this approach become the standard for distributing and executing single-file Python tools?


Comments are closed.