Amiverse

uv: A More Modern Python Package Manager

Install uv:

brew install uv

Check uv version:

uv -- version

Clear cache files:

uv cache clean

Manage Python versions:

  • Show all available Python versions in the current environment:
uv python list

This command lists every usable Python version, including all Python interpreters inside your conda virtual environments.

  • Install a specific Python interpreter version:
uv python install 3.12

This command downloads the specified interpreter version to the directory below:

/Users/username/.local/share/uv/python
  • Set the Python version to use:
uv python pin 3.13

This command sets the default Python version used by subsequent uv commands. If a Python 3.13 interpreter exists in a conda virtual environment, uv will use that interpreter from conda. If conda is not available, it will use the interpreter managed by uv under /Users/username/.Local/share/uv/python.

  • Confirm the Python version in the current environment:
uv run python --version
  • Confirm the path of the Python interpreter in the current environment:
cd uv_test
uv run which python

Initialize a project

cd uv_test
uv python pin 3.13
uv init
  • uv init will look for a matching Python 3.13 interpreter in local cache based on your pinned Python version (3.13).

  • If it is missing locally (or incomplete), it will automatically download the corresponding Python 3.13 from official sources (to /Users/username/.Local/share/uv/python).

  • After download completes, uv init will create a .venv/ virtual environment under ~/work/uv_test/.

  • The Python interpreter inside .venv/ is exactly that official, newly downloaded Python 3.13 version.

  • The interpreter files are actually stored in cache (/Users/username/.Local/share/uv/python), while the Python inside .venv/ is a symlink or copy.

  • If you switch to another project and pin a different Python version, uv will automatically download and manage different versions in isolation.

Install packages

uv add requests==version

Packages are installed into .venv/lib/python3.13/site-packages. The virtual environment created by uv uses an isolated package directory instead of directly using Conda's site-packages. This aligns well with Python virtual environment design: shared interpreter, isolated packages.

  • Packages you install with uv add (such as requests, certifi, etc.) are all installed into this isolated .venv/lib/python3.13/site-packages directory;

  • In other words, even if the Python interpreter may come from a Conda environment, packages are installed into your project's dedicated virtual environment directory and isolated from other Conda environments.

If I want to publish an open-source project, I used to include requirements.txt so others could see dependencies. How should this be done with uv?

When you manage project dependencies with uv, the recommended way to declare and publish dependencies in open-source projects is slightly different from the traditional requirements.txt approach.


Review of the traditional approach
  • Traditionally with pip, you place a requirements.txt at the project root.

  • Others install dependencies in one shot with pip install -r requirements.txt.


Recommended approach with uv
1. Use uv.lock to lock dependencies
  • When you install dependencies with uv add <package>, uv automatically generates a uv.lock file (similar to poetry.lock or Pipfile.lock).

  • This file records package versions, hashes, and more in detail, ensuring others can reproduce the exact same dependency environment.

2. Commit uv.lock into the project
  • Add uv.lock to your Git repository (do not ignore it).

  • After others pull the code, they run: uv sync

  • uv sync installs dependency versions that exactly match uv.lock.

3. Export requirements.txt

If you want users who use pip to know project dependencies, you can export a requirements..txt file:

uv export --format requirements-txt --no-dev -o requirements.txt
4. Install dependencies of a project managed by uv

If you clone a uv-managed project and want to install all dependencies, just run:

uv synv

This is similar to the old pip install -r reqiurements.txt.

If I get a project managed by requirements.txt, how can I use uv to install dependencies?

If you receive a project managed with requirements.txt and want to install dependencies using uv, follow these steps:


1. Create and enter the project directory
`cd /path/to/project`

Make sure you are at the project root where requirements.txt exists.


2. Create a virtual environment with uv
uv init

This creates a .venv virtual environment for you, using either the system Python or your pinned Python version by default.


3. Import requirements.txt with uv import

uv provides an import command that can convert dependencies from requirements.txt into uv's management format:

uv add -r requirements.txt

After running:

  • uv reads dependencies from requirements.txt.

  • Generates a corresponding uv.lock (version lock).

  • Installs all dependencies into .venv.


4. Install dependencies

Usually uv import installs dependencies automatically. If not, you can run:

uv sync

Use uv.lock to install dependencies and ensure consistent versions.

Check which dependency packages exist in the current environment

cd uv_test
uv pip list

At this point, make sure your command line does not have any conda environment activated.

#Tech