python-uv
Run Python scripts with automatic dependency management using uv. Use this skill when you need to (1) run Python scripts with external dependencies without polluting system Python, (2) create standalone Python scripts with inline dependencies (PEP 723), (3) convert existing scripts to use uv with inline metadata, (4) handle ModuleNotFoundError by running scripts with uv run --with, or (5) create CLI tools, data processing scripts, or API clients that are self-contained and portable.
SKILL.md
| Name | python-uv |
| Description | Run Python scripts with automatic dependency management using uv. Use this skill when you need to (1) run Python scripts with external dependencies without polluting system Python, (2) create standalone Python scripts with inline dependencies (PEP 723), (3) convert existing scripts to use uv with inline metadata, (4) handle ModuleNotFoundError by running scripts with uv run --with, or (5) create CLI tools, data processing scripts, or API clients that are self-contained and portable. |
name: python-uv description: Run Python scripts with automatic dependency management using uv. Use this skill when you need to (1) run Python scripts with external dependencies without polluting system Python, (2) create standalone Python scripts with inline dependencies (PEP 723), (3) convert existing scripts to use uv with inline metadata, (4) handle ModuleNotFoundError by running scripts with uv run --with, or (5) create CLI tools, data processing scripts, or API clients that are self-contained and portable.
Python uv Script Management
Overview
This skill helps create and run Python scripts with automatic dependency management using uv tool. It provides two approaches: inline dependencies (PEP 723) for new/converted scripts, and uv run --with for running existing scripts without modification.
Quick Decision Guide
When to use each approach:
- Inline Dependencies (PEP 723) → Creating new scripts or converting scripts for distribution
uv run --with→ Running existing third-party scripts or quick one-off execution
Approach 1: Inline Dependencies (Recommended)
When to Use
- Creating new standalone scripts
- Scripts that will be shared or distributed
- Long-term maintainable scripts
- Scripts with known, stable dependencies
Basic Template
#!/usr/bin/env -S uv run --script
# /// script
# dependencies = [
# "package-name>=version",
# ]
# requires-python = ">=3.11"
# ///
import package_name
# Your code here
Creating New Scripts
Use the template assets based on your use case:
- CLI tool →
assets/template-cli.py- Command-line tools with argument parsing - Data processing →
assets/template-data.py- Pandas-based data analysis - API client →
assets/template-api.py- REST API integration with auth
Copy the appropriate template and customize for your needs.
Run with: ./script.py or uv run script.py
Converting Existing Scripts
Use the utility script to add inline dependencies:
scripts/add_inline_dependencies.py <script.py> <dep1> [dep2] ...
Example:
scripts/add_inline_dependencies.py myscript.py "requests>=2.31.0" "pyyaml>=6.0"
This will:
- Add PEP 723 metadata block with dependencies
- Update shebang to
#!/usr/bin/env -S uv run --script - Make the script executable
- Preserve original code
Approach 2: uv run --with for Old-Style Scripts
When to Use
- Running existing scripts without modifying them
- Third-party scripts you can't or don't want to modify
- Quick testing with different dependency versions
- When encountering
ModuleNotFoundErrorfor unmaintained scripts
Pattern
When you see import errors:
ModuleNotFoundError: No module named 'yaml'
Solution:
uv run --with pyyaml script.py
Multiple dependencies:
uv run --with pyyaml --with requests --with pandas script.py
Common Import-to-Package Mappings
| Import Statement | Package Name |
|---|---|
import yaml | pyyaml |
import PIL | pillow |
import bs4 | beautifulsoup4 |
import dotenv | python-dotenv |
For complete mapping table and more details, see references/uvx-guide.md
Example Workflows
Workflow 1: Create New CLI Tool
- Copy template:
cp assets/template-cli.py mytool.py - Customize the script for your use case
- Make executable:
chmod +x mytool.py - Run:
./mytool.pyoruv run mytool.py
Workflow 2: Convert Existing Script
- Identify missing dependencies from imports
- Run:
scripts/add_inline_dependencies.py oldscript.py "dep1>=1.0" "dep2>=2.0" - Test:
./oldscript.py
Workflow 3: Quick Fix for Module Errors
- Encounter:
ModuleNotFoundError: No module named 'requests' - Run:
uv run --with requests script.py - If recurring, consider converting to inline dependencies
Best Practices
- Always specify version constraints (e.g.,
"requests>=2.31.0") for reproducibility - Pin Python version if using newer language features
- Use
--scriptflag in shebang to enable inline dependency support - Test after conversion to ensure all dependencies are captured
- Prefer inline dependencies for scripts you maintain or distribute
- Use
uv run --withfor quick experiments or external scripts
Resources
scripts/
add_inline_dependencies.py- Add PEP 723 metadata to existing Python scripts
references/
uvx-guide.md- Comprehensive guide with examples, troubleshooting, and detailed explanations
assets/
template-cli.py- CLI tool template with Clicktemplate-data.py- Data processing template with Pandastemplate-api.py- API client template with Requests
Troubleshooting
Script not found: Install uv with brew install uv or curl -LsSf https://astral.sh/uv/install.sh | sh
Module not found during execution: Use uv run --with package-name script.py for quick fix, or add to inline dependencies for permanent solution
Permission denied: Run chmod +x script.py to make executable
For more detailed troubleshooting, see references/uvx-guide.md.