I was setting up Claude Code hooks recently and ran into the usual Python dependency mess again: Create a virtual environment, activate it, install packages, cross your fingers that nothing breaks.
The Problem: Dependency Management Friction
Here’s what my hook scripts looked like before:
|
|
|
|
This works, but it’s painful. Scripts aren’t portable. New machines need setup. Colleagues can’t just run your code. It’s 2025 and we’re still doing this.
The Discovery: PEP 723 and UV
While researching UV (Ultra-fast Python package installer), I found something interesting. Python now has an official standard for inline script metadata: PEP 723. And UV implements it.
Here’s the same hook script with inline dependencies:
|
|
The magic? I can run this directly:
|
|
UV reads the inline metadata, creates a temporary virtual environment, installs dependencies, and executes the script. The first run takes a few hundred milliseconds. Subsequent runs are blazing fast thanks to UV’s cache.
The AI Connection: Claude Already Knows This
Here’s where it gets interesting. I discovered that Claude 4 already understands PEP 723 syntax. I can ask:
“Write a Python script with inline dependencies that fetches GitHub repo stats”
And Claude generates working code with proper inline dependencies. I save it and run uv run script.py
- it just works. No environment setup, no dependency installation, no conflicts.
Performance in Practice
I tested this with a GitHub API script that fetches repo data and formats it with rich tables:
|
|
The cache makes a huge difference. UV is written in Rust and is fast - often 10-100x faster than pip.
Teaching AI to Use UV
While researching UV workflows, I discovered an interesting technique from Armin Ronacher (Flask creator) for training AI agents to use UV instead of system Python. The idea is to create interceptor scripts that give helpful error messages:
Create Interceptor Scripts
|
|
Setup with direnv
|
|
Now when AI agents try to run python script.py
, they get a helpful error message explaining to use uv run
instead. This trains them to use UV automatically in your projects.
A Note on AI-Generated Code
When using AI-generated scripts, I always review the code before running (always review AI code before running):
- Check dependencies - Are they legitimate packages?
- Review file operations - What files is it accessing?
- Examine network calls - Where is it connecting?
- Validate inputs - How does it handle user data?
UV makes it easy to see exactly what dependencies a script needs upfront.
Troubleshooting Tips
Python Version Conflicts
|
|
Dependency Issues
|
|
Wrapping Up
UV single-file scripts solve the Python dependency portability problem. It is one less thing to worry about. :)
This eliminates the traditional environment management overhead while keeping dependency precision - particularly valuable when working with AI tools where you want to iterate quickly.