---
title: "How to speed up Python CI with uv, Ruff, and pytest-xdist"
description: "Speed up Python CI with uv for installs, Ruff for lint and format, faster type checkers, parallel pytest, wheels instead of source builds, and Granian."
url: https://9apes.com/blog/speed-up-python-ci/
---

# How to speed up Python CI with uv, Ruff, and pytest-xdist

To speed up Python CI, install dependencies with uv from a lockfile, replace Flake8, isort, and Black with Ruff, run pytest in parallel with pytest-xdist, and make sure every dependency installs from a prebuilt wheel. In most Python pipelines, environment setup and the test run are the entire critical path, and each of those changes shortens one of them.

This post is part of a series on [speeding up CI](/blog/speed-up-ci-builds/). The hub covers techniques that apply to every language; this one covers Python.

## Install with uv

[uv](https://github.com/astral-sh/uv) is a Python package and project manager written in Rust by Astral. It replaces pip, pip-tools, virtualenv, and pyenv in one binary, and its README describes it as 10 to 100 times faster than pip. The [benchmarks](https://github.com/astral-sh/uv/blob/main/BENCHMARKS.md) include a cold-cache case that the uv team describes as equivalent to a fresh CI machine.

A complete setup in GitHub Actions:

```yaml
- uses: actions/checkout@v7
- uses: astral-sh/setup-uv@v10.3.3
  with:
    python-version: "3.14"
    enable-cache: true
- run: uv sync --locked --all-extras --dev
- run: uv run pytest -n auto
- run: uv cache prune --ci
```

Three details matter:

- **`--locked`** fails if `uv.lock` is missing or out of date, so CI never resolves dependencies silently. `--frozen` skips that check entirely; use `--locked` unless you have a reason not to.
- **`uv cache prune --ci`** removes downloaded wheels from the cache before it is saved but keeps wheels built from source. The [uv cache docs](https://docs.astral.sh/uv/concepts/cache/#caching-in-continuous-integration) explain why: downloading prebuilt wheels again is often faster than restoring them from a cache, while source builds are expensive to repeat.
- **Cache poisoning protection.** Since setup-uv v10, the default `enable-cache: auto` turns caching off for `pull_request_target`, `workflow_run`, and `release` events. Do not turn it back on there without understanding why.

uv can also install Python itself with `uv python install`. The [uv GitHub guide](https://docs.astral.sh/uv/guides/integration/github/) notes that `actions/setup-python` can be faster on GitHub-hosted runners, which keep common versions in their tool cache. On other runners, uv's download is usually the quicker path.

## Lint and format with Ruff

[Ruff](https://github.com/astral-sh/ruff) replaces Flake8 and dozens of its plugins, isort, pyupgrade, and Black, with one tool written in Rust. Astral's benchmark, linting the CPython codebase from scratch, puts it 10 to 100 times ahead of existing linters:

```yaml
- run: uvx ruff check --output-format=github .
- run: uvx ruff format --check .
```

`--output-format=github` turns violations into inline annotations on the pull request. On most codebases this job finishes in seconds, so it can run first and fail the pipeline before anything slow starts.

## Type check with a faster checker

mypy and Pyright remain the reference type checkers, and on large codebases they are often the slowest job in a Python pipeline. Two Rust-based checkers now compete with them:

- **[ty](https://github.com/astral-sh/ty)**, from Astral, claims 10 to 100 times faster checks than mypy and Pyright. It is in beta, and its diagnostics may change between any two versions.
- **[Pyrefly](https://github.com/facebook/pyrefly)**, from Meta, reached 1.0 in May 2026 and describes itself as stable. Meta reports checking PyTorch 15 times faster than mypy and Pyright.

Neither reports exactly the same errors as mypy, because type checkers differ in inference and in how strictly they read the spec. A safe migration runs the new checker as a non-blocking job alongside the old one, triages the differences, and switches the required check once the output is stable. Pin the checker version, because a new release can add errors to code that passed yesterday.

## Run pytest in parallel

[pytest-xdist](https://github.com/pytest-dev/pytest-xdist) distributes tests across worker processes:

```bash
uv run pytest -n auto --dist worksteal
```

- `-n auto` starts one worker per physical core; `-n logical` uses logical cores.
- `--dist loadscope` keeps tests from the same module or class on one worker, so expensive module- or class-scoped fixtures are set up once.
- `--dist worksteal` rebalances work when test durations vary widely.

Parallel tests need isolated state. Tests that share a database, a port, or a temporary directory will fail intermittently under xdist; give each worker its own resources using the `worker_id` fixture.

### Shard across jobs

When one machine is not enough, [pytest-split](https://github.com/jerry-git/pytest-split) divides the suite across matrix jobs using recorded test durations:

```bash
# once, locally or on a schedule; commit the result
uv run pytest --store-durations

# in each of three matrix jobs
uv run pytest --splits 3 --group ${{ matrix.group }}
```

Each shard installs the environment again, which uv makes cheap. Combining both, xdist within each shard, is common for suites that run for tens of minutes.

## Install wheels, never build from source

A dependency without a wheel for your platform and Python version is compiled from source on every cold install, sometimes taking minutes and requiring a C compiler. This often happens right after a new Python release, before every package publishes wheels for it.

Make it fail loudly instead:

```bash
uv sync --locked --no-build
```

`--no-build` refuses to build any source distribution; `--no-build-package <name>` limits it to specific packages. If a package has no wheel, pin a version that does, or keep the built wheel in your cache: `uv cache prune --ci` keeps source-built wheels precisely for this case.

## Reduce startup and import time

Large test suites import a lot of code before running a single test. [Python 3.15](https://docs.python.org/3.15/whatsnew/3.15.html), released in October 2026, adds explicit lazy imports (PEP 810): `lazy import module` defers loading until first use, and `-X lazy_imports=all` applies it globally. For test runs and CLIs dominated by import time, that can remove a large share of startup. Measure with `python -X importtime` first to see whether imports are your problem.

## Where Granian fits

[Granian](https://github.com/emmett-framework/granian) is an HTTP server for Python applications written in Rust on top of Hyper and Tokio. It serves ASGI, WSGI, and its own RSGI interface, and it is designed for request throughput, HTTP/2, and websockets in production.

Granian is a runtime choice, not a CI accelerator. It will not make your tests run faster on its own. It becomes relevant to CI when your end-to-end or integration tests boot the real application server: running the same server in tests as in production removes a class of "works with the dev server" bugs, and its embeddable mode runs workers as asyncio tasks, which can simplify in-process test fixtures. We are not aware of published measurements of its startup time in CI, so measure your own server boot before assuming a saving.

## How 9apes speeds up Python CI

With uv and Ruff, setup and linting take seconds, and the remaining time is the test run. Python tests mostly run single-threaded per worker, so per-core speed decides how long each test takes, and core count decides how many run at once under xdist.

9apes runs each GitHub Actions job in a fresh microVM on hardware chosen for high single-core performance with NVMe storage. Fast cores shorten each test and the import-heavy startup of every worker; NVMe speeds up unpacking thousands of small files into a virtual environment. Runners go up to 8 vCPU on Linux x86_64, so `-n auto` gets more workers without sharding across jobs. See [available runners](/docs/available-runners/).

We have not published a Python-specific benchmark; the [9apes benchmarks](/benchmarks/) cover compiled C++, Rust, and Go builds. To measure your own suite, follow the [quick start](/docs/quick-start/), change the runner label, and compare complete job time and [cost](/pricing/):

```diff
- runs-on: ubuntu-latest
+ runs-on: 9apes-8vcpu-ubuntu-2404
```

## Is uv faster than pip in CI?

Yes, typically by a large margin, especially with a lockfile and a warm cache. Use `uv sync --locked`, and prune the cache with `--ci` so restoring it does not cost more than downloading.

## Should I cache the virtual environment in GitHub Actions?

Usually not. Cache uv's package cache and recreate the environment with `uv sync`, which is fast enough that restoring a full virtual environment rarely wins.

## Can ty or Pyrefly replace mypy?

They can for many projects, but they do not report identical errors. Run the new checker alongside mypy until the differences are understood, then switch.

## Does Granian make Python CI faster?

Not directly. It is a production HTTP server. It is relevant when integration tests boot your application server, and any startup saving should be measured.
