uv - .venv & Package Manager

Mastering uv by Astral(founder: Charlie Marsh). An ultra fast alternative to pip and conda for modern Python development.

Jan 11, 2026

๐Ÿš€ uv โ€“ Modern Python Package & Environment Manager

uv is a fast, modern, and opinionated Python package manager designed to replace pip, virtualenv, pip-tools, and partially even poetry.

It is written in Rust, which means it is:

  • Extremely fast
  • Deterministic
  • Reproducible by default

โ“ Why uv?

Problems with the traditional Python workflow

  • Manual virtual environment creation and activation
  • Slow dependency resolution
  • requirements.txt does not guarantee reproducibility
  • Different environments across machines
  • Global installation of tools like ruff, black, etc.

What uv fixes

  • โšก Very fast dependency resolution (Rust-based)
  • ๐Ÿง  Automatic virtual environment management
  • ๐Ÿ”’ Reproducible builds using pyproject.toml & uv.lock

๐Ÿ“ฆ Installation

Official documentation:
https://docs.astral.sh/uv/getting-started/installation/


๐Ÿงฑ ๐ŸŸ  Initialize uv in the new or current existing project

(Automatically Creates Virtual Environment โ€” No Manual Setup)

uv automatically creates and manages the .venv for you.
You never need to activate or configure it manually.



๐Ÿ”น SYNTAX

To initialze uv in a new project(with default available python version)

uv init app_name

OR

To initialze uv in a new project(with specific/particular python version)

uv init app_name -p <python-version>

OR

To Initialize uv in the existing project(with specific python version)

uv init -p <python-version>

OR

To Initialize uv in the existing project(without specific python version)

uv init .

๐Ÿ”น EXAMPLES

โžก๏ธ Default project structure

uv init new_app --app

OR

uv init -p 3.14.2

OR

uv init -p 3.14.2 .

OR

uv init --python 3.14.2 .

โžก๏ธ Build app as a Python package

uv init new_app --lib


๐ŸŸข Install partiuclar python version that you need inside uv environment

๐Ÿ”น Syntax
uv python install <version>
๐Ÿ”น Example
uv python install 3.13.3

To list the installed python versions in uv environment

๐Ÿ”น EXAMPLES:

To check all the installed(downloaded) python in the uv

uv python list

To check the current running version of python inside uv

uv run py -V

OR

uv run python --version

To uninstall :-

๐Ÿ”น Example
uv python uninstall 3.13.3


Creating virtual environment (.venv)

There are two ways to create .venv:-

One Way:- Creating .venv without any dependencies/packages installed.

    uv venv

Another Way :- Creating .venv automatically while adding/installing dependencies/packages.

Syntax:-

    uv add package_name

Example

    uv add "fastapi[standard]"
    uv add pydantic
    uv add sqlalchemy
    v add psycopg[binary]


๐Ÿ“ uv directory structure

uv directory structure

uv directory structure



๐Ÿ” Initializing uv in cloned remote project

๐Ÿ”„ Syncing Environment

Use this when: - you have to synchronize .venv (or create a .venv) for a project.

How "uv sync" command works?

  • It looks for the pyproject.toml file and then creates uv.lock file.
  • Now, using uv.lock file it creates .venv directory.
  • It is mostly used command.

uv sync --> searches pyproject.toml --> creates uv.lock ---> creates .venv (install all dependencies)

๐Ÿ”น Command

uv sync

๐Ÿ”’ Locking / Freezing Environment

How "uv lock" command works?

  • It looks for the pyproject.toml file and then creates uv.lock file, but doesn't creates .venv (no dependencies installation).
  • It is rarely used command.

uv lock --> searches pyproject.toml --> creates uv.lock (listing all minor dependencies without installation)

uv.lock file

uv.lock file



โ™ป๏ธ Migrate existing pip managed project to uv managed

If you already have a project created using pip and want to move it to uv:

โœ… No need to create a new project

โœ… No need to migrate manually

๐Ÿชœ Step 1: Initialize uv

uv init

๐Ÿชœ Step 2: Install all packages from requirements.txt

uv add -r requirements.txt


๐Ÿ’ป Run project using uv

There are two ways to run project:-


๐Ÿ’ก One Way:- Use uv run command each time.

๐Ÿ”น Syntax

uv run project_file.py

๐Ÿ”น Example 1

uv run main.py 

If you are using django and you want to run server.

๐Ÿ”น Example 2

uv run python manage.py runserver 

๐Ÿ”€ Another Way :- Activate .venv manually and run command without uv

Activate virtual environment

For Windows:

.venv\Scripts\activate

For Linux:

source .venv/bin/activate

Run project file

๐Ÿ”น Example 1

python main.py 

If you are using django and you want to run server.

๐Ÿ”น Example 2

python manage.py runserver 

๐Ÿ Your existing project is now fully managed by uv.



โž• Add a Package Using uv

๐Ÿ”น Syntax

uv add package_name

OR

uv add package_name1 package_name2 ... 

๐Ÿ”น Example

uv add flask

OR

uv add pandas flask django


โž– Remove Packages/Dependencies


Remove specific packages

๐Ÿ”น Syntax

uv remove package_name

๐Ÿ”น Example

uv remove flask

Remove all packages

Step 1 : Manually remove all dependencies from pyproject.toml

[project]
name = "new-app"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13.3"
dependencies = []                                       #empty dependencies list

Step 2 : Regenerates uv.lock with zero runtime dependencies.

uv lock

Step 3: Sync the environment

uv sync

This sync everything.



๐ŸŒณ Visualize Dependency Tree

See exactly which package depends on what:

uv tree

Helpful when debugging dependency conflicts.




ยฎ๏ธ Running Tools with uvx

โ“What is uvx? Why it is required?

uvx is a tool runner, similar to:

  • pipx
  • npx

It's job :

  • Run CLI applications (like streamlit, black, ruff, pytest)
  • Install them temporarily or globally

Use uvx when:

  • Youโ€™re running a tool
  • The tool has a CLI

uvx does one core thing:

  • Run a Python CLI tool via its entry point, in an isolated environment.

There are two different method to run tools:-

๐Ÿ”’ Installs the tool into a persistent tool environment managed by uv

What it actually means :-

  • Installs the tool into uv-managed tool environment, in a global location under your user home directory.

~/.local/share/uv/

  • NOT your project .venv
  • Creates a stable CLI command on your PATH

Consequences :-

  • Tool is installed once
  • Reused across projects

๐Ÿ”น Syntax

uv tool install tool_name

๐Ÿ”น Example Command

uv tool install ruff 

Installs ruff into uvโ€™s tool environment and exposes ruff on your PATH.

๐Ÿ”นRun the tool

ruff check


BONUS / TIPS

๐Ÿ“Œ What uv python pin actually does?

  • Creates/updates .python-version
  • Tells uv which interpreter to run commands with.

๐Ÿ”น Syntax

uv python pin python_version

๐Ÿ”น Example

uv python pin 3.13.3

โœ… Summary

  • Automatic virtual environments
  • Clean dependency management
  • Reproducible builds via uv.lock
  • Temporary tooling without system pollution
  • Simple, fast, and reliable Python workflow

WALLAAHH !!