In this lesson, you will learn how to rapidly initialize new Python projects and manage isolated environments using uv, a high-performance alternative to traditional environment management tools.
Why uv for Bootstrapping?
In your previous experience with venv or pipenv, you likely spent time manually creating directories, running python -m venv .venv, and activation scripts. While these are necessary for isolation, they can be slow and repetitive. uv streamlines this by acting as a drop-in replacement that performs these tasks significantly faster, often by orders of magnitude.
Initializing a Project
To start a new project from scratch, you don't need to manually configure the structure. You can use the uv init command to generate a standardized project scaffold immediately:
uv init my-project
cd my-project
This creates a pyproject.toml file and a boilerplate directory structure. Unlike older workflows, uv treats the environment as a first-class citizen integrated into the tool itself.
Adding Dependencies
Once your project is initialized, you need to manage your dependencies. Instead of running a series of commands to create an environment, activate it, and then run pip install, you can simply use uv add:
uv add requests
When you run this, uv automatically:
- Detects or creates a virtual environment if one doesn't exist.
- Resolves the dependencies for
requests. - Installs the packages into your
.venv. - Updates your
pyproject.tomland generates auv.lockfile to ensure your environment is reproducible and locked to specific versions.
Running Code in the Environment
Because uv manages the environment transparently, you don’t need to worry about the "activation" ritual required by venv. You can execute your scripts using the environment’s context directly:
uv run main.py
This command ensures that main.py runs using the interpreter and the exact packages defined in your project, maintaining isolation without the overhead of manually managing shell states. By shifting to uv, you replace a fragmented process of manual environment creation and resolution with a unified, lightning-fast command-line interface.
Key Takeaway: uv simplifies project bootstrapping by unifying environment creation, dependency resolution, and execution into a single, high-performance CLI that eliminates the need for manual virtual environment activation.