Django: set up its test suite with uv

When contributing to Django, you’ll want to run its test suite to check your changes. The official contributing docs cover setup in Running the unit tests, using a virtual environment and pip. Here’s an alternative guide using uv.
Setup
First, clone Django and change into the repository:
$ git clone https://github.com/django/django
$ cd django
(You can fork it later if you want to make a pull request.)
Then, create a virtual environment:
$ uv venv
Using CPython 3.14.6
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate
uv automatically picks a compatible Python version for you, downloading one if needed.
Next, install Django in editable mode, plus the test requirements, in one command:
$ uv pip install -e . -r tests/requirements/py3.txt
uv pip automatically targets the .venv directory, so there’s no need to activate it first.
Run the tests
Activate the virtual environment, change into the tests directory, and run runtests.py:
$ source .venv/bin/activate
$ cd tests
$ ./runtests.py
Testing against Django installed in '/.../django/django' with up to 8 processes
...
Skip pylibmc if it won’t build
The test requirements include pylibmc, a memcached client that wraps the libmemcached C library. In my experience, it’s the biggest sticking point when setting up Django’s test suite, as installing pylibmc means compiling it, which means you need a C compiler and libmemcached’s headers correctly installed. If the compilation fails, you’ll see an error like:
src/_pylibmcmodule.h:42:10: fatal error: libmemcached/memcached.h: No such file or directory
Rather than fixing the environment, it’s often easier to skip installing pylibmc entirely. This is safe because Django will skip any tests that require pylibmc, so you can still run the rest of the test suite.
The official docs suggest commenting out pylibmc’s line in requirements/py3.txt, but that leaves you with a modified file to avoid committing. Instead, filter it out on the fly with grep -v and shell process substitution:
$ uv pip install -e . -r <(grep -v pylibmc tests/requirements/py3.txt)
Then run the tests as above, and Django will skip any that need pylibmc.
Fin
Now uv got a great base to contribute to Django!
—Adam
Read my book Boost Your Django DX, freshly updated in November 2024.
One summary email a week, no spam, I pinky promise.
Related posts: