ar3ph.github.io

Debloating Python Setup

If you have lots of Python projects to manage, in addition to your python-based utilities like httpie and jupyter, chances are you have lots of virtualenvs and let them take up space everywhere. Virtualenvs are not as notorious as node_modules, but they are pretty close.

On the other hand, you might use pyenv to manage your Python versions. Pyenv compiles Python for every installation, which is an annoyance, and it takes pyenv a really long time to compile and install Python on slow machines.

How do you solve these problems? One year ago I’d probably recommend you to try rye, download precompiled Python on your own, or just get a better computer. Nowadays I’ll just say one word: uv.

uv can download prebuilt Pythons from GitHub, and use hardlinks for package management to make packages use less disk space. You can follow the official guide to install uv. Here are a few examples of using uv:

Use or Install Utils

If you have installed uv, you will have an extra utility uvx. Uvx runs the latest version of a package, like below:

1uvx ruff # runs the latest version of ruff

Of course, you can pass command line arguments to the package:

1uvx ruff format

And to install ruff to $HOME/.local/bin (which should be in your $PATH, so you can directly run the ruff executable file in your shell), use the following command:

1uv tool install ruff

As a drop-in replacement for pip

Now relax, because uv has great backward compatibility. uv can do almost everything pip does. Instead of invoking pip, now you use uv pip to do pip things. Most pip-tools features are also supported, such as pip-compile (as uv pip compile) and pip-sync (as uv pip sync).

To install dependencies from requirements.txt, run

1uv pip install -r requirements.txt

, to generate requirements.txt:

1uv pip freeze > requirements.txt

and to compile dependencies from requirements.in (a pip-tools feature):

1uv pip compile requirements.in -o requirements.txt

Start a new project with uv

uv can manage projects, too. You can create a python package with the following command:

1uv init your_package

And you should create a virtual package instead if you don’t expect to install your project with pip install or uv add

1uv init --virtual your_project

When you use uv to manage your project, you are able to switch python verions:

1uv python pin 3.13

, add dependencies more robustly than pip install:

1uv add numpy

, create virtualenv with hardlinks:

1uv venv

and run anything with virutalenv enabled:

1uv run manage.py migrate

To add a dependency used for development:

1uv add --dev django-extensions

After others clone your project, they should run the following to reinstall dependencies to their environment:

1uv sync

, and use the following to ignore development dependencies while installing:

1uv sync --no-dev

#Linux #Python