Python for AIPython for AI
Tools

Using uv

Create projects the modern way

Get ready to learn about uv, a super-fast Python package installer and dependency resolver. It's an awesome tool for managing your Python projects efficiently.

Creating a new project

Start a new project by making a directory. This keeps your project files neat and organized.

bash
mkdir my_new_project
cd my_new_project

Now, initialize uv in your new project. This sets up everything you need to manage your dependencies.

bash
uv init

Understanding pyproject.toml

When you initialize a project with uv, it creates a pyproject.toml file. This is where your project's settings and dependencies live.

text
# pyproject.toml
[project]
name = "my-new-project"
version = "0.1.0"

[tool.uv.sources]

[tool.uv.scripts]

You'll define your project's name, version, and the packages it needs here. It's a central hub for all project-related configuration.

Adding packages

Let's add a package to your project. Most projects use a testing framework like pytest.

bash
uv add pytest

uv will download pytest and its dependencies. It also updates your pyproject.toml file automatically.

The lock file

uv creates a uv.lock file. This file records the exact version of every package your project uses.

The lock file ensures that everyone working on the project uses the exact same versions of dependencies, preventing 'it works on my machine' problems.

You should always commit your uv.lock file to version control (like Git). This way, other developers can install the exact same environment.

Running your code

After adding packages, you'll want to run your Python scripts. uv handles the virtual environment for you.

bash
uv run python your_script.py

This command executes your_script.py within the project's isolated environment. No need to manually activate a virtual environment.

python
# your_script.py
import pytest

def greet():
    return "Hello, uv!"

print(greet())

Common uv commands

Here are some of the most useful uv commands you'll use regularly:

  • uv add <package>: Add a new package to your project.
  • uv remove <package>: Remove an installed package.
  • uv update: Update all installed packages to their latest compatible versions.
  • uv run <command>: Execute a command within the project's virtual environment.

Working with existing projects

If you join an existing project that uses uv, getting started is easy. Clone the repository and install the dependencies.

bash
git clone my-awesome-project
cd my-awesome-project
uv sync

uv sync reads the uv.lock file and installs all listed packages with their exact versions. This guarantees you have the exact same setup as other developers.

Virtual environment details

uv automatically creates and manages a virtual environment inside your project directory. It's usually in a hidden folder, something like .venv.

You generally don't need to interact directly with the virtual environment folder. uv run and other commands handle it for you.

This isolation ensures that your project's dependencies don't conflict with other Python projects on your machine.

Tips and tricks

  • Always use uv add to install new packages, not pip install.
  • Remember to commit your pyproject.toml and uv.lock files.
  • If you encounter issues, uv clean can sometimes fix them by removing cached data.

For more advanced usage and configuration, check out the official uv documentation.

What's next?

You've successfully learned the basics of uv! You can now create new projects, add dependencies, and run your code with confidence.

Explore more Python libraries, build your first web application, or dive deeper into testing. uv will be there to help manage your project's needs.