Skip to main content

Overview

Python scripts are single files intended for standalone execution. uv manages script dependencies automatically without requiring manual environment management.
Every Python installation has an environment for packages. uv automatically creates isolated virtual environments for scripts and prefers declarative dependencies.

Running scripts without dependencies

Execute any Python script with uv run:
example.py

Scripts with standard library imports

No special setup needed for standard library modules:
example.py

Passing arguments

Pass arguments to your script after the script name:
example.py

Reading from stdin

If you use uv run in a project directory (with pyproject.toml), use --no-project to skip installing the project:

Running scripts with dependencies

When your script requires external packages, declare dependencies explicitly. uv creates environments on-demand instead of using long-lived virtual environments.

Requesting dependencies per invocation

For a script requiring rich:
example.py
Request the dependency with --with:
Add version constraints if needed:
If used in a project, --with dependencies are included in addition to project dependencies. Use --no-project to opt-out.

Declaring script dependencies

1

Initialize a script with metadata

Create a script with inline metadata using the PEP 723 format:
2

Add dependencies

Use uv add --script to declare dependencies:
This adds a script section at the top of your file:
example.py
3

Run the script

uv automatically creates an environment with the required dependencies:
When using inline script metadata, the dependencies field must be provided even if empty. Project dependencies are ignored — no --no-project flag needed.

Python version requirements

Specify required Python versions in the script metadata:
example.py
uv will search for and download the required Python version if it’s not installed.

Using a shebang for executable scripts

Make scripts executable without uv run:
1

Create script with shebang

greet
2

Make executable

3

Run directly

Shebang with dependencies

example

Advanced features

Using alternative package indexes

Specify a custom package index:
This includes the index in the inline metadata:

Locking dependencies

Explicitly lock script dependencies:
This creates example.py.lock adjacent to your script. Subsequent operations reuse the locked dependencies.

Improving reproducibility

Limit packages to those released before a specific date:
example.py
The date should be an RFC 3339 timestamp.

Using different Python versions

Request arbitrary Python versions per invocation:
example.py

GUI scripts (Windows)

On Windows, scripts with .pyw extension run using pythonw:
example.pyw
Works with dependencies too:

Complete example

Here’s a complete workflow for creating a self-contained script:
1

Initialize the script

2

Add dependencies

3

Write your code

weather.py
4

Run the script

5

Lock for reproducibility

Next steps

Command reference

View all uv run options

Using tools

Run and install Python tools

Python versions

Learn about Python version management

Working with projects

Manage multi-file Python projects