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:
By default uv starts an application project, with a src/<project_name>/ structure. See more about types of projects below.
When starting a new project you can specify the Python version using:
Types of projects
Application projects are suitable for web servers, scripts, and command-line interfaces. This is the default project type (corresponds to the --app flag).
example-app/
├─ .python-version
├─ pyproject.toml # includes [build-system]
├─ README.md
└─ src/
└─ example-app/
└─ __init__.py
A build system is defined in pyproject.toml, so the project will be installed into the environment
Library projects provide functions and objects for other projects to consume. Libraries are intended to be built and distributed, e.g., by uploading them to PyPI.
example-lib/
├─ .python-version
├─ pyproject.toml # includes [build-system]
├─ README.md
└─ src/
└─ example-lib/
├─ py.typed
└─ module.py # your library code
└─ __init__.py
As with a packaged application, a src layout is used. A py.typed marker is included to indicate to consumers that types can be read from the library. A src layout ensures that the library is isolated from any python invocations in the project root and that distributed library code is well separated from the rest of the project source. A build system is defined, so the project will be installed into the environment.
No package projects disable using a build system.
More info here.
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: