Working with uv
uv
is a Python package and project manager. It is extremely fast, compared with alternatives.
Installing
On Windows, you can install uv
using Powershell:
More info on other installation options can be found on the uv docs.
To update uv:
Starting a new project
Starting a project with uv
:
uv
creates a new directory called project_name
and sets up a few project files. You can then enter the project folder.
Note: If you already have a project folder you can ask uv
to initialize the project there:
When starting a new project you can specify the Python version using:
Installing packages
To add packages:
To remove packages:
Working with development dependencies:
Running scripts
To run python files in you environment use:
Working with tools
Tools are pacakges that can perform several functions but are not part of your project. For instance, it is common to use a linter/formatter when developing (e.g., ruff
). With uv
you can use tools like ruff
in different ways.
Ways to use tools
1) Running a tool without installing it (it's installed in a temporary environment and deleted after use).
2) When a tool is used frequently it may be usefull to install it to a persistent environment and add it to the PATH
.
ruff
by doing:
3) You can also install the tool in your project as a development dependency
And run it withUpgrading tools
Upgrade all tools with:
Or a single tool with:
Working with jupyter
If you're working within a project, you can start a Jupyter server with access to the project's virtual environment by:
Alternatively you can install Jupyter as a dev dependency in your project
Project entry points and command line interfaces
To create a project CLI you need to configure entry point tables in the pyproject.toml
and add a build system. For example, to declare a command called hello
that invokes the hello
function in the example
module:
In the pyproject.toml
file:
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project.scripts]
hello = "example:hello"
You can then run the command with:
Deploying
Deploying the project to production excluding dev dependencies: